Spring Boot Embedded Servers - Tomcat, Jetty and Undertow



In this article, we will discuss what is default spring boot embedded web server, how to change the embedded server and how to configure the embedded server.
Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers. Most developers use the appropriate “Starter” to obtain a fully configured instance. By default, spring boot embedded tomcat server, which listens for HTTP requests on port 8080.
Spring Boot supports the following embedded servlet containers:
You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.

1. Spring Boot Default Embedded Server - Tomcat

We use spring-boot-starter-web starter to create Spring boot web applications or Rest API development. The spring-boot-starter-web stater project internally provides below dependency in its pom.xml:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <version>2.0.5.RELEASE</version>
     <scope>compile</scope>
</dependency>
You can see that by default Spring boot web starter includes a dependency on starter tomcat.
Again spring-boot-starter-tomcat starter has the following dependencies.
<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-core</artifactId>
     <version>8.5.23</version>
     <scope>compile</scope>
</dependency>
<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-el</artifactId>
     <version>8.5.23</version>
     <scope>compile</scope>
</dependency>
<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-websocket</artifactId>
     <version>8.5.23</version>
     <scope>compile</scope>
</dependency>
The spring-boot-starter-tomcat starter brings in all the dependencies need to run Tomcat as an embedded server.

2. Use Another Web Server

As we knew that, for servlet stack applications, the spring-boot-starter-web includes Tomcat by including spring-boot-starter-tomcat, but you can use spring-boot-starter-jetty or spring-boot-starter-undertow instead.
When switching to a different HTTP server, you need to exclude the default dependencies in addition to including the one you need. Spring Boot provides separate starters for HTTP servers to help make this process as easy as possible.
Let's look at how to configure Jetty and Undertow web servers.

2.1 Using Jetty as Embedded Server with Spring Boot

The below Maven example shows how to exclude Tomcat and include Jetty for Spring MVC:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Exclude the Tomcat dependency -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

2.2 Using Undertow as Embedded Server with Spring Boot

The below Maven example shows how to exclude Tomcat and include Undertow for Spring MVC:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
         <!-- Exclude the Tomcat dependency -->
        <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-undertow</artifactId>
</dependency>

3. Disabling the Web Server

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behaviour configure the web application type in your application.properties, as shown in the following example:
spring.main.web-application-type=none

4. Change the HTTP Port

In a standalone application, the main HTTP port defaults to 8080 but can be set with server.port in application.properties:

Now the server will start on port http://localhost:8081.
Similarly, we can do the same if we’re using an application.yml file:
server:
  port: 8081
Both files are loaded automatically by Spring Boot if placed in the src/main/resources directory of a Maven application.
Check out this article to know how to change default port and context path in spring boot applications.

5. Discover the HTTP Port at Runtime

We can access the port the server is running on from log output or from the ServletWebServerApplicationContext through its WebServer. For example, below test case that uses @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) can also inject the actual port into a field by using the @LocalServerPort annotation, as shown in the following example:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyWebIntegrationTests {

    @Autowired
    ServletWebServerApplicationContext server;

    @LocalServerPort
    int port;

    // ...
}
Note that @LocalServerPort is a meta-annotation for @Value("${local.server.port}").

6. Enable HTTP Response Compression

HTTP response compression is supported by Jetty, Tomcat, and Undertow. It can be enabled in application.properties, as follows:
server.compression.enabled=true
By default, responses are compressed only if their content type is one of the following:
  • text/html
  • text/xml
  • text/plain
  • text/css
  • text/javascript
  • application/javascript
  • application/json
  • application/xml

7. Configure SSL [HTTPS]

Self-Signed Certificate (SSL) can be configured declaratively by setting the various server.ssl.* properties, typically in application.properties or application.yml. The following example shows setting SSL properties in application.properties:
server.port=8443
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
server.ssl.key-password=another-secret
Read more about how to configure SSL in Spring boot applications at https://www.baeldung.com/spring-boot-https-self-signed-certificate

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments