Spring Boot is a widely used framework used for building Java applications. It comes with lots of features that make a developer’s life easy. Out of those features, spring boot applications come with an embedded server in it. The default server being used in spring boot applications is Apache Tomcat, you can use any other server of your choice like Jetty, Undertow, etc.
But sometimes we don’t need this feature of an embedded server and we may have some requirements which do not require a web server like:
- Batch Processing Applications using Spring Batch
- Application running scheduled tasks
- Command line utilities
- Message Queue Consumer Application
- Stream Processing
Generally, these kinds of applications are not client-facing applications and these are there to support the services of our infrastructure. One example of a scenario is when we create this type of application while creating a notification application which consumes data from message queue, send notification. It does not involve any client interaction.
We are here going to see the ways of creating a spring boot application without web server. We will first of all create a spring boot application from Spring Initializer.
Creating a Spring Boot Application Without a Web ServerPrerequisites:- JDK 17 or above
- IntelliJ Idea or your other favorite IDE
Step 1: Creating a Spring Boot ApplicationCreate a spring boot application from Spring Initializer with these set of configurations provided below. Add the dependency of spring web by clicking on add dependencies button.
Refer the below image of Spring Initializr for creating the Spring Boot project.

pom.xml file:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springWithoutWebServer</groupId>
<artifactId>springWithoutWebServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springWithoutWebServer</name>
<description>Spring Boot project without web server</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Ways to Remove the Embedded Web ServerWay 1: Web Server Dependency ExclusionIn this existing application, we are having the dependency of spring starter web. This starter dependency comes with the dependency of tomcat web server. We have to exclude this dependency of tomcat from the starter dependency of spring web in pom.xml file.
You can do the exclusion like mentioned below:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> After exclusion, your pom.xml should look like this:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springWithoutWebServer</groupId>
<artifactId>springWithoutWebServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springWithoutWebServer</name>
<description>Spring Boot project without web server</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Way 2: Updating application.properties /application.yml
Add this given property in applicaton.properties /application.yml file.
application.properties:
spring.main.web-application-type=none or,
application.yml:
spring: main: web-application-type: none Way 3: Updating the Main Method
Customize the main method using SpringApplicationBuilder to set the application type to NONE :
Java
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SpringWithoutWebServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SpringWithoutWebServerApplication.class)
.web(WebApplicationType.NONE)
.run(args);
}
}
In this article, we have discussed different ways to create our application without web server. These methods are useful for creating applications that do not require client interactions.
|