Configure Jetty Server in Spring Boot

Spread the love

By default, Spring boot uses an embedded tomcat server to run the application. At times, you may need to use the jetty server in place of the tomcat server. Spring Boot provides Tomcat and Jetty dependencies bundled together as separate starters to help make this process as easy as possible. You can use jetty with following simple steps.

Add spring-boot-starter-jetty dependency

You will need to update pom.xml and add a dependency for spring-boot-starter-jetty. Also, you will need to exclude default added spring-boot-starter-tomcat dependency.


Configure Jetty Options

To override, default jetty runtime configuration – you can configure them in file.

####Jetty specific properties########
server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content.
server.jetty.selectors= # Number of selector threads to use.

Also, you may configure these options programmatically using JettyServletWebServerFactory and ConfigurableServletWebServerFactory.

public ConfigurableServletWebServerFactory webServerFactory() 
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
    return factory;