Spring Boot application without web component

Use cases

  1. console applications
  2. job scheduling
  3. batch or stream processing
  4. serverless applications
  5. and more

Implementation

Using dependencies

Do not include the spring-boot-starter-web in the dependencies. Instead, use the more basic spring-boot-starter dependency.

Spring boot will not include embedded tomcat if you don’t have Tomcat dependencies on the classpath.

https://github.com/spring-projects/spring-boot/blob/v1.1.7.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java

The meat of the code is the use of the @ConditionalOnClass annotation on the class EmbeddedTomcat.

It is possible for Tomcat dependencies to be included in our application as transitive dependencies. In this case, we might need to exclude the Tomcat library from whichever dependency is including it.

Disable using configuration

For version 2.0.0 of Spring boot starter, use the following property :

spring.main.web-application-type=none

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/WebApplicationType.html

https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html

Cons of using this approach

If you have any of these dependencies,

spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-starter-jetty
spring-boot-starter-tomcat
spring-boot-starter-jdbc
spring-boot-starter-data-rest
...

these dependencies pack spring-boot-starter-tomcat which will then bring the dependency of tomcat-embed-core which is the actual tomcat server. This library is packed automatically inside the jar and is of size ~3.3 MB. Even if you disable the server with the aforementioned property, you will still deliver your application jar file, containing the tomcat server inside.

Alternative

If you want to exclude Tomcat from the jar file, exclude it in the POM like this:

<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>

How to deploy these applications?

These applications need to be deployed in stand-alone servers like Tomcat, Websphere, JBoss, or any of the enterprise application servers.

Accidentally omitting spring-boot-starter-web from spring-boot applications

What happens if we forget to include this dependency?

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot application will shutdown as soon as the application starts.

[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.8)

2024-05-13 16:22:52.534  INFO 1601240 --- [           main] c.e.swaggerpocjava8spring27.Application  : Starting Application using Java 18.0.2 on explorer436-p50-20eqs27p03 with PID 1601240 (/home/explorer436/Downloads/GitRepositories/programming-playground/java-playground/openapi-and-swagger/swagger-poc-java8-spring2.7/target/classes started by explorer436 in /home/explorer436/Downloads/GitRepositories/programming-playground/java-playground/openapi-and-swagger/swagger-poc-java8-spring2.7)
2024-05-13 16:22:52.536  INFO 1601240 --- [           main] c.e.swaggerpocjava8spring27.Application  : No active profile set, falling back to 1 default profile: "default"
2024-05-13 16:22:53.018  INFO 1601240 --- [           main] c.e.swaggerpocjava8spring27.Application  : Started Application in 0.761 seconds (JVM running for 0.994)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.071 s
[INFO] Finished at: 2024-05-13T16:22:53-04:00
[INFO] ------------------------------------------------------------------------
[explorer436@explorer436-p50-20eqs27p03 swagger-poc-java8-spring2.7]$

Links to this note