使用Spring Boot配置Jetty服务器

x33g5p2x  于2022-09-30 转载在 Spring  
字(1.8k)|赞(0)|评价(0)|浏览(680)

默认情况下,Spring boot 使用 Tomcat 作为嵌入式 Web 服务器。 但是,如果您需要某些特定功能,还有其他可用的 Web 服务器。 在本教程中,我们将学习如何使用 Jetty 作为 Web 服务器。

添加 spring-boot-starter-jetty 依赖

您将需要更新 pom.xml 并为 spring-boot-starter-jetty 添加依赖项。 此外,您需要排除默认添加的 spring-boot-starter-tomcat 依赖项,如下所示:

<?xml version="1.0" encoding="UTF-8"?><project>
   <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>
    
</project>

如果您使用 Gradle 作为构建工具,则可以使用以下方法获得相同的结果:

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:2.1.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-jetty:2.1.0.RELEASE")
}

配置Jetty

可以通过 application.properties 文件覆盖默认配置来配置 Web 服务器
`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.

此外,您可以使用 ConfigurableServletWebServerFactoryJettyServletWebServerFactory 类以编程方式配置这些选项:

@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;
  }

相关文章

微信公众号

最新文章

更多