Spring Boot Without A Web Server
Last Updated :
03 Jul, 2024
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 Server
Prerequisites:
- JDK 17 or above
- IntelliJ Idea or your other favorite IDE
Step 1: Creating a Spring Boot Application
Create 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 Server
Way 1: Web Server Dependency Exclusion
In 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.
Similar Reads
Spring Boot - Admin Server
Spring boot is one of the most popular applications to develop Java enterprise applications. When you have so many components in your application, you need to monitor and manage them. You have multiple spring boot applications running you need an admin for monitoring. Spring boot admin provides you
4 min read
Spring Boot - Web Socket
WebSocket is used as a communication protocol that provides full-duplex communication channels over a single, long-lived connection between a client and a server. In this protocol, there are no restrictions like HTTP that for a response you have to make a request first. The server can send the messa
10 min read
Spring Boot - Eureka Server
In modern microservices architectures, service registration and discovery are crucial components that enable seamless communication between different services. Netflix's Eureka Server, integrated with Spring Boot, provides an elegant solution for managing these aspects. In this article, we will dive
4 min read
Spring Boot - Sending SMS with Twilio
Spring is a widely used framework of Java that allows us to create stand-alone and robust applications at the enterprise level. Spring boot is a tool or an extension of the Spring framework that makes developing web applications and microservices much faster and easier. It offers an integrated serve
6 min read
Spring Boot with H2 Database
H2 Database in Spring Boot is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It stores data in memory, not persist the data on disk. Here we will be discussing how can we configure and perform some b
6 min read
Spring Boot Setup with Kotlin
Spring Boot is one of the best frameworks available to build full-stack enterprise applications. Initially, Spring was used to build stand-alone applications on the Java Platform supporting easy-to-develop and providing lots of handy features to ease the application development. Why Kotlin is Used?T
5 min read
Serve Static Resources With Spring
It's essential to know about static resources, which are the resources that do not change frequently on a web page. To serve these static resources, Spring Boot comes with an inbuilt class called ResourceHttpRequestHandler in conjunction with the ResourceHandlerRegistry class. You can keep these sta
4 min read
What is Spring Data REST?
Spring Data REST is a framework that exposes Spring Data repositories as RESTful endpoints. It allows you to create REST APIs for your data without having to write any controller code. Under the hood, Spring Data REST uses Spring Data repositories to access and manage data. So you first need to defi
7 min read
Spring Boot - Caching with Redis
Caching is a crucial optimization technique used to enhance the performance and scalability of web applications. It temporarily stores data in cache memory to reduce access time and load on backend systems. Redis (Remote Dictionary Server) is a popular open-source, in-memory data structure store use
7 min read
How to Create a Spring Boot Project?
Spring Boot is built on top of the spring and contains all the features of spring. It is one of the most popular frameworks for building Java-based web applications and microservices. It is a favorite among developers due to its rapid, production-ready environment, which allows developers to focus o
6 min read