Running Spring Boot Application on Jetty Server
Follow these instructions to create a simple spring boot application using Spring Tool Suite(STS). The steps are similar for eclipse IDE.
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,
buildscript { ext { springBootVersion = '1.5.2.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } compile('org.springframework.boot:spring-boot-starter-jetty') 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.
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.
# modify the default port on which Jetty listens for requests server.port = 9090 # modify the default context path server.context-path= /hello # Number of acceptor threads to use. server.jetty.acceptors= 5 # Maximum size in bytes of the HTTP post or put content. server.jetty.max-http-post-size=1000000 # Number of selector threads to use. server.jetty.selectors=10
See this link 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.
package com.quickprogrammingtips.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @Controller public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } }