Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Spring Boot is a great framework for quickly creating new Java applications for a variety of use cases. One of the most popular uses is as a web server, using one of the many supported embedded servlet containers and template engines.

However, Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, and more.

In this tutorial, we’ll look at several different ways to use Spring Boot without a web server.

2. Using Dependencies

The easiest way to prevent a Spring Boot application from starting an embedded web server is to not include the web server starter in our dependencies.

This means not including the spring-boot-starter-web dependency in either the Maven POM or Gradle build file. Instead, we would want to use the more basic spring-boot-starter dependency in its place.

Keep in mind it’s 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.

3. Modifying the Spring Application

Another way to disable the embedded web server in Spring Boot is by using code. We can use either the SpringApplicationBuilder:

new SpringApplicationBuilder(MainApplication.class)
  .web(WebApplicationType.NONE)
  .run(args);

Or we can use the reference to the SpringApplication:

SpringApplication application = new SpringApplication(MainApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);

In either case, we have the benefit of keeping the servlet and container APIs available on the classpath. This means we can still use the web server libraries without starting the web server. This can be useful, for example, if we want to use them to write tests or use their APIs in our own code.

4. Using Application Properties

Using code to disable the web server is a static option — it will affect our application no matter where we deploy it. But what if we want to create the web server in specific circumstances?

In this case, we can use Spring application properties:

spring.main.web-application-type=none

Or using the equivalent YAML:

spring:
  main:
    web-application-type: none

The benefit of this approach is we can conditionally enable the web server. Using Spring profiles or conditionals, we can control the web server behavior in different deployments.

For example, we could have the web server running in development only to expose metrics or other Spring endpoints while keeping it disabled in production for security reasons.

Note that some earlier versions of Spring Boot used a boolean property named web-environment to enable and disable the web server. With the adoption of both traditional and reactive containers in Spring Boot, the property has been renamed and now uses an enum.

5. Conclusion

There are many reasons for creating Spring Boot applications without a web server. In this tutorial, we’ve seen multiple ways to do this. Each has its own pros and cons, so we should pick the approach that best meets our needs.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.