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,
3 |
springBootVersion = '1.5.2.RELEASE' |
9 |
classpath( "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" ) |
14 |
apply plugin: 'eclipse' |
15 |
apply plugin: 'org.springframework.boot' |
17 |
version = '0.0.1-SNAPSHOT' |
18 |
sourceCompatibility = 1.8 |
25 |
compile( 'org.springframework.boot:spring-boot-starter-web' ) { |
26 |
exclude group: 'org.springframework.boot' , module: 'spring-boot-starter-tomcat' |
28 |
compile( 'org.springframework.boot:spring-boot-starter-jetty' ) |
29 |
testCompile( 'org.springframework.boot:spring-boot-starter-test' ) |
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 |
4 |
# modify the default context path |
5 |
server.context-path= /hello |
7 |
# Number of acceptor threads to use. |
8 |
server.jetty.acceptors= 5 |
10 |
# Maximum size in bytes of the HTTP post or put content. |
11 |
server.jetty.max-http-post-size=1000000 |
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; |
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; |
11 |
public class DemoApplication { |
13 |
public static void main(String[] args) { |
14 |
SpringApplication.run(DemoApplication. class , args); |
20 |
return "Hello World!" ; |
Reference:
spring boot web application
spring boot jetty
spring boot official