1.jetty介绍
2.配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- json库统一使用fastjson -->
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<!-- 去除tomcat容器 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 增加jetty容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
3.相关参数配置(线程池之类的)
配置Jetty参数
我们可以在application.properties配置文件里配置相关参数,去覆盖Jetty默认使用的运行参数:
application.properties
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.
同样,我们可以通过JettyEmbeddedServletContainerFactory
bean以编程的方式去配置这些参数
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory jettyContainer =
new JettyEmbeddedServletContainerFactory();
jettyContainer.setPort(9000);
jettyContainer.setContextPath("/home");
return jettyContainer;
}
Spring boot 2.0.0.RELEASE版本之后的更新
以上代码针对的是Spring Boot Snapshot版本,在Spring boot 2.0.0.RELEASE版本之后,我们应该使用 ConfigurableServletWebServerFactory
和 JettyServletWebServerFactory
类去配置Jetty参数:
创建ConfigurableServletWebServerFactory
Bean
@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;
}