🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Spring Boot – 配置 Jetty 服务器 > 原文: [https://howtodoinjava.com/spring-boot/configure-jetty-server/](https://howtodoinjava.com/spring-boot/configure-jetty-server/) 默认情况下,Spring Boot 使用嵌入式 tomcat 服务器来运行应用程序。 有时,您可能需要使用 [Jetty](https://www.eclipse.org/jetty/) 服务器来代替 tomcat 服务器。 Spring Boot 将 Tomcat 和 Jetty 依赖项捆绑在一起,作为单独的启动器,以帮助简化此过程。 您可以通过以下简单步骤使用 Jetty。 ## 添加`spring-boot-starter-jetty`依赖 您将需要更新`pom.xml`并添加对`spring-boot-starter-jetty`的依赖。 另外,您将需要排除默认添加的`spring-boot-starter-tomcat`依赖。 ```java <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> ``` 在 gradle 中,可以通过以下方式实现有效的更改: ```java configurations { compile.exclude module: "spring-boot-starter-tomcat" } dependencies { compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT") compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT") } ``` ## 配置 Jetty 选项 要覆盖,默认 Jetty 运行时配置 – 您可以在`application.properties`文件中对其进行配置。 `application.properties` ```java 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 以编程方式配置这些选项。 ```java @Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory(); jettyContainer.setPort(9000); jettyContainer.setContextPath("/home"); return jettyContainer; } ``` #### Spring Boot 2.0.0.RELEASE 的更新 上面的代码段对 spring boot spanshot 版本有效。 在 Spring boot `2.0.0.RELEASE`可用之后,您将使用[`ConfigurableServletWebServerFactory`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.html)和[`JettyServletWebServerFactory`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.html)类。 `Create bean of type ConfigurableServletWebServerFactory` ```java @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; } ``` 将您的问题留给我,有关在任何 spring boot 应用程序中使用 jetty 代替 tomcat 服务器的问题。 学习愉快! 参考:[配置 Jetty](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-another-web-server)