HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot / Spring Boot – Configure Jetty Server

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.

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.

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

Ref: Configure Jetty

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Rudolf Flütsch

    June 1, 2018

    I am looking for a way to configure Jetty only to listen on 127.0.0.1.
    I found the option to be used within the “normal” jetty configuration, i.e. using jetty.http.host.

    Unfortunately I have not found a way to do this using the spring configuration file or programatically – any idea how to do this ?

  2. Karl

    April 3, 2018

    Doesn’t seem to work with version 2.0.0.RELEASE. Gives error:

    Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

    • Lokesh Gupta

      April 4, 2018

      This works in spring boot 2.0.0.RELEASE.

      @Bean
      public ConfigurableServletWebServerFactory webServerFactory() 
      {
      	JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
      	factory.setPort(9000);
      	factory.setContextPath(&quot;/myapp&quot;);
      	factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, &quot;/notfound.html&quot;));
      	return factory;
      }
      
  3. vikas

    October 26, 2017

    I have a spring boot application (version 2.0) reactive web one , i by defaultt have the tomcat connected , what should i do if need to connect to tomcat and another server say jetty at the same time by modifying the application.properties , say like if i choose tomcat it should start up with tomcat and other way around .?

  4. Markus Schulte

    May 17, 2017

    I linked your example, how to configure Jetty programmatically, at https://stackoverflow.com/questions/20454441/how-to-configure-jetty-in-spring-boot-easily/44021864#44021864

    • Lokesh Gupta

      May 17, 2017

      Thanks Markus !!

Comments are closed on this article!

Search Tutorials

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces