W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

Spring Boot – Configure Jetty Server

By default, Spring boot uses embedded tomcat server to run the application. At times, you may need to use jetty server in place of 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 dependency for spring-boot-starter-jetty. Also, you will need to exclude default added spring-boot-starter-tomcat dependency.

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

In gradle, able change can be achieved by this:

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT")
    compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT")
}

Configure Jetty Options

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

application.properties
server.port=8080
server.servlet.context-path=/home
 
####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 programatically using JettyEmbeddedServletContainerFactory bean.

@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
    JettyEmbeddedServletContainerFactory jettyContainer =
        new JettyEmbeddedServletContainerFactory();
      
    jettyContainer.setPort(9000);
    jettyContainer.setContextPath("/home");
    return jettyContainer;
}

Update for Spring boot 2.0.0.RELEASE

Above code snippet was valid for spring boot spanshot version. After Spring boot 2.0.0.RELEASE is available, you shall be using ConfigurableServletWebServerFactory and JettyServletWebServerFactory classes.

Create bean of type ConfigurableServletWebServerFactory
@Bean
public ConfigurableServletWebServerFactory webServerFactory()
{
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.setPort(9000);
    factory.setContextPath("/myapp");
    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
    return factory;
}

Drop me your questions in comments section related to using jetty in stead of tomcat server in any spring boot application.

Happy Learning !!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.