How To Run Spring Boot Application On Jetty

Running Spring Boot App on Jetty Server

The default embedded application server used by spring boot is tomcat. When a spring boot starter project is created using the spring-boot-starter-web dependency, it includes an embedded tomcat instance as well. To use Jetty as the embedded server, add a dependency to the spring-boot-starter-jetty project by modifying the build.gradle file. We will also remove the spring-boot-starter-tomcat dependency from the spring-boot-starter-web dependency since it is no longer needed. Here is the modified build.gradle file,

1 buildscript {
2     ext {
3         springBootVersion = '1.5.2.RELEASE'
4     }
5     repositories {
6         mavenCentral()
7     }
8     dependencies {
9         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10     }
11 }
12  
13 apply plugin: 'java'
14 apply plugin: 'eclipse'
15 apply plugin: 'org.springframework.boot'
16  
17 version = '0.0.1-SNAPSHOT'
18 sourceCompatibility = 1.8
19  
20 repositories {
21     mavenCentral()
22 }
23  
24 dependencies {
25     compile('org.springframework.boot:spring-boot-starter-web') {
26         exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'  
27     }
28     compile('org.springframework.boot:spring-boot-starter-jetty')
29     testCompile('org.springframework.boot:spring-boot-starter-test')
30 }

To verify the changed dependencies, run the dependencies gradle task. All the dependencies of the project will be printed on the console.

gradle dependencies

Configuring Embedded Jetty Server

It is possible to change various parameters of jetty server by specifying them in the application.properties file located in src/main/resources folder.

1 # modify the default port on which Jetty listens for requests
2 server.port = 9090
3  
4 # modify the default context path
5 server.context-path= /hello
6  
7 # Number of acceptor threads to use.
8 server.jetty.acceptors= 5
9  
10 # Maximum size in bytes of the HTTP post or put content.
11 server.jetty.max-http-post-size=1000000
12  
13 # Number of selector threads to use.
14 server.jetty.selectors=10

spring boot app example for a full list of server properties configurable in spring boot.

Right click the build.gradle from IDE and click on Gradle => Refresh Gradle Project. Run the application using the STS IDE or use the gradle bootRun task from command line. Browse the URL http://localhost:9090/hello to access the spring boot application.

Following is the spring boot entry point class used in the above example. The home method prints "Hello World!" on the browser window.

1 package com.quickprogrammingtips.springboot;
2  
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.stereotype.Controller;
6 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.ResponseBody;
8  
9 @SpringBootApplication
10 @Controller
11 public class DemoApplication {
12  
13     public static void main(String[] args) {
14         SpringApplication.run(DemoApplication.class, args);
15     }
16  
17     @RequestMapping("/")
18     @ResponseBody
19     String home() {
20         return "Hello World!";
21     }
22 }

 

Reference:

spring boot web application

spring boot jetty

spring boot official