How can you disable the embedded server in Spring Boot?

How can you disable the embedded server in Spring Boot?

A) Remove the server dependency from the classpath
B) Set the server port to 0 in the application.properties
C) Set the server port to -1 in the application.properties
D) Use @SpringBootApplication without @EnableAutoConfiguration

Answer:

A) Remove the server dependency from the classpath

Explanation:

To disable the embedded server in Spring Boot, you can remove the server dependency (such as Tomcat, Jetty, or Undertow) from the classpath. This is done by excluding the embedded server dependency in your project configuration, typically in the pom.xml file for Maven projects or the build.gradle file for Gradle projects.

Here’s how you can exclude Tomcat in a Maven project:


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

By excluding the embedded server, Spring Boot will not start the web application in an embedded container. This is useful for applications that do not require a web server, such as those that only need to perform background processing or communicate via messaging systems.

Alternatively, you can set the server port to 0 in the application.properties file to disable the server, but this is less common and may not work in all scenarios. The most reliable method is to exclude the server dependency entirely.

Reference links:

Spring Boot Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top