SpringExamples

Spring boot embedded jetty server example

By Admin | Filed Under: Spring Boot

By default, spring boot uses embedded tomcat server to run the applications. You can use embedded jetty server to launch your applications – in two simple steps:

  1. Exclude tomcat server dependency
  2. Include jetty server dependency

1. Embedded Jetty Server Configuration Example

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.springexamples.demo</groupId>
		<artifactId>SpringExamples</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.springexamples.demo</groupId>
	<artifactId>embedded-jetty-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>embedded-jetty-server</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<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>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

2. Jetty Server Demo

Now run the application and observe the console output.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-05-05 19:55:03.344  INFO 3764 --- [           main] com.springexamples.demo.Application      : Starting Application on user-PC with PID 3764 (C:\Users\user\git_springexamples\SpringExamples\embedded-jetty-server\target\classes started by user in C:\Users\user\git_springexamples\SpringExamples\embedded-jetty-server)
2018-05-05 19:55:03.458  INFO 3764 --- [           main] com.springexamples.demo.Application      : No active profile set, falling back to default profiles: default
2018-05-05 19:55:03.615  INFO 3764 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.ser[email protected]e136d3: startup date [Sat May 05 19:55:03 IST 2018]; root of context hierarchy
2018-05-05 19:55:04.802  INFO 3764 --- [           main] org.eclipse.jetty.util.log               : Logging initialized @2336ms to org.eclipse.jetty.util.log.Slf4jLog
2018-05-05 19:55:05.002  INFO 3764 --- [           main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8080
2018-05-05 19:55:05.004  INFO 3764 --- [           main] org.eclipse.jetty.server.Server          : jetty-9.4.9.v20180320; built: 2018-03-20T17:51:10+05:30; git: 1f8159b1e4a42d3f79997021ea1609f2fbac6de5; jvm 1.8.0_20-ea-b05
2018-05-05 19:55:05.284  INFO 3764 --- [           main] org.eclipse.jetty.server.session         : DefaultSessionIdManager workerName=node0
2018-05-05 19:55:05.284  INFO 3764 --- [           main] org.eclipse.jetty.server.session         : No SessionScavenger set, using defaults
2018-05-05 19:55:05.318  INFO 3764 --- [           main] org.eclipse.jetty.server.session         : Scavenging every 660000ms
2018-05-05 19:55:05.356  INFO 3764 --- [           main] o.e.j.s.h.ContextHandler.application     : Initializing Spring embedded WebApplicationContext
2018-05-05 19:55:05.356  INFO 3764 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1745 ms
2018-05-05 19:55:05.500  INFO 3764 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-05-05 19:55:05.503  INFO 3764 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-05 19:55:05.503  INFO 3764 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-05 19:55:05.503  INFO 3764 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-05 19:55:05.503  INFO 3764 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-05 19:55:05.507  INFO 3764 --- [           main] o.e.jetty.server.handler.ContextHandler  : Started [email protected]{/,[file:///C:/Users/user/AppData/Local/Temp/jetty-docbase.8385296376636463789.8080/],AVAILABLE}
2018-05-05 19:55:05.508  INFO 3764 --- [           main] org.eclipse.jetty.server.Server          : Started @3044ms
2018-05-05 19:55:05.631  INFO 3764 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-05 19:55:05.934  INFO 3764 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.ser[email protected]e136d3: startup date [Sat May 05 19:55:03 IST 2018]; root of context hierarchy
2018-05-05 19:55:06.021  INFO 3764 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-05 19:55:06.023  INFO 3764 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-05 19:55:06.050  INFO 3764 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-05 19:55:06.050  INFO 3764 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-05 19:55:06.266  INFO 3764 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-05 19:55:06.284  INFO 3764 --- [           main] o.e.j.s.h.ContextHandler.application     : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-05 19:55:06.284  INFO 3764 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-05 19:55:06.297  INFO 3764 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms
2018-05-05 19:55:06.343  INFO 3764 --- [           main] o.e.jetty.server.AbstractConnector       : Started [email protected]{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2018-05-05 19:55:06.346  INFO 3764 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 8080 (http/1.1) with context path '/'
2018-05-05 19:55:06.351  INFO 3764 --- [           main] com.springexamples.demo.Application      : Started Application in 3.49 seconds (JVM running for 3.886)
Download Sourcecode

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Development Environment

  • Spring Boot 2.0.1.RELEASE
  • Maven
  • JDK 8
  • Eclipse

Spring Boot Tutorial

  • Hello world example
  • Which main class
  • Print all beans
  • Embedded jetty server
  • CommandLineRunner
  • Custom banner
  • Change Root Path and Server Port

Spring Tutorial

  • IoC Container
  • Java Configuration

Spring WebMVC Tutorial

  • Minimum required config

Copyright © 2016 · springexamples.com · All Rights Reserved. | Sitemap