Can you disable the default web server in the Spring Boot application?

Disabling the Default Web Server in a Spring Boot Application

If you want to disable the default web server in a Spring Boot application, there are multiple ways to do it. However, before going into that, let's first understand what is meant by the default web server in a Spring Boot application.

Understanding the Default Web Server

When you create a Spring Boot application, it comes with an embedded web server, which is used to serve your web content. The default web server that is included in a Spring Boot application is Apache Tomcat.

This embedded web server is included to make it easier for developers to get started with creating web applications. However, in some cases, you may want to disable the default web server and use a different web server instead. For example, you may want to use Nginx or Apache HTTP Server.

Disabling the Default Web Server

There are multiple ways to disable the default web server in a Spring Boot application:

  • Using command-line arguments: You can disable the embedded web server by using the --server.port=-1 command-line argument when running your application. This will prevent the embedded web server from starting.
  • Using configuration properties: You can also disable the embedded web server by setting the spring.main.web-application-type=none configuration property in your application.properties (or application.yml) file. This will prevent the embedded web server from starting.
  • Using code: You can also disable the embedded web server by adding the @EnableAutoConfiguration(exclude = {EmbeddedServletContainerAutoConfiguration.class}) annotation to your main Spring Boot application class. This will exclude the embedded web server configuration from being loaded.

Conclusion

Disabling the default web server in a Spring Boot application can be useful in some cases, such as when you want to use a different web server for your application. There are multiple ways to do it, including using command-line arguments, configuration properties, and code.


@SpringBootApplication
@EnableAutoConfiguration(exclude = {EmbeddedServletContainerAutoConfiguration.class})
public class MySpringBootApp {

  public static void main(String[] args) {
    SpringApplication.run(MySpringBootApp.class, args);
  }

}

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe