SoFunction
Updated on 2025-04-13

How to replace Tomcat container with Jetty container

Tomcat container replaced with Jetty container

To replace the default Tomcat container with a Jetty container in your Spring Boot app, follow these steps:

Modify Maven dependencies

  • Exclude Tomcat dependencies: First of all, you need toIn the filespring-boot-starter-webTomcat is excluded from dependencies. This can be done<exclusions>Tag implementation.
<dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId></groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
  • Add Jetty dependencies:Next,Add Jetty's starting dependency.
<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Configuration file adjustment (optional)

Although usually the above steps are enough, if you need to make additional configuration for Jetty, you canAdd the corresponding configuration item to it.

For example, adjust the port:

# Port Configuration=8081

# Number of startup threads=2

# Selector thread count=4

# Access log configuration=true
=
-date-format=.yyyy-MM-dd

# SSL/TLS configuration-store=classpath:keystore.p12
-store-type=PKCS12
-store-password=changeit
=tomcat

# Request and response buffer size-http-header-size=10KB
-http-form-post-size=20MB

Rebuild and run

After completing the above modifications, make sure to rebuild your project so that Maven downloads new dependencies and updates your app configuration. If you use an IDE, you can usually do this by cleaning and rebuilding projects. After that, when you run your Spring Boot app again, it will use Jetty as a web container instead of Tomcat.

Since the actual log content will vary depending on your specific environment (such as Spring Boot version, Jetty version, specific application configuration, etc.), I will provide a simplified example to illustrate the log output that may occur when a Spring Boot application is started using Jetty.

Note that the following example is based on text descriptive construction and does not come directly from a particular running instance.

:: Spring Boot ::                (v2.6.3)

2023-04-09T10:30:45.00+08:00 INFO [main] - Starting application using Java 11.0.13 on DESKTOP-XXXXXX with PID 12344 (C:\workspace\myapp\target\classes started by UserName in C:\workspace\myapp)
2023-04-09T10:30:45.23+08:00 DEBUG [main] - Application failed to start with classpath: [file:/C:/workspace/myapp/target/classes/, ...]

2023-04-09T10:30:46.54+08:00 INFO [main] - Starting Jetty web server...
2023-04-09T10:30:46.75+08:00 INFO [main] - Logging initialized @139ms to .Slf4jLog
2023-04-09T10:30:46.90+08:00 INFO [main] - jetty-9.4.44.v20210927; built: 2021-09-27T11:06:34.143Z; git: 4a1c13d268d4dfe655fb413a2f4aabb09bb33357; jvm 11.0.13+8
2023-04-09T10:30:46.92+08:00 INFO [main] - Started @764973b: http://0.0.0.0:8080/
2023-04-09T10:30:47.02+08:00 INFO [main] - Started Server@7e2f09d in 2037ms

2023-04-09T10:30:47.03+08:00 INFO [main] - Started application in 2.49 seconds (JVM running for 3.266)

This sample log shows several key steps in Spring Boot application startup, including Spring Boot startup information, Jetty server initialization, and the final successful startup message, including the bound port (in this example, 8080).

Please note that date and timestamp, PID, username, path, version number and other information will vary according to the actual situation.

Summarize

The above steps should help you successfully replace the Tomcat container in your Spring Boot application with a Jetty container.

These are just personal experience. I hope you can give you a reference and I hope you can support me more.