使用Spring Boot切换不同容器(Tomcat、Jetty、Undertow)的配置,分别列举application.properties、application.yml、pom.xml 的配置示例。
1、application.properties配置示例
1、切换到Tomcat容器:properties
spring.main.web-application-type=SERVLET
server.servlet.container=org.apache.catalina.startup.Tomcat
2、切换到Jetty容器:properties
spring.main.web-application-type=SERVLET
server.servlet.container=org.eclipse.jetty.server.Server
3、切换到Undertow容器:properties
spring.main.web-application-type=SERVLET
server.servlet.container=io.undertow.servlet.core.DeploymentManagerImpl
2、application.yml配置示例
1、切换到Tomcat容器:
spring:
main:
web-application-type: SERVLET
servlet:
container: org.apache.catalina.startup.Tomcat
2、切换到Jetty容器:
spring:
main:
web-application-type: SERVLET
servlet:
container: org.eclipse.jetty.server.Server
3、切换到Undertow容器:
spring:
main:
web-application-type: SERVLET
servlet:
container: io.undertow.servlet.core.DeploymentManagerImpl
3、pom.xml配置示例
1、切换到Tomcat容器
<properties>
<java.version>1.8</java.version>
<tomcat.version>9.0.41</tomcat.version>
</properties>
<dependencies>
<!-- Tomcat容器 -->
<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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
</dependencies>
2、切换到Jetty容器
<properties>
<java.version>1.8</java.version>
<jetty.version>9.4.40.v20210413</jetty.version>
</properties>
<dependencies>
<!-- Jetty容器 -->
<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.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
3、切换到Undertow容器
<properties>
<java.version>1.8</java.version>
<undertow.version>2.2.5.Final</undertow.version>
</properties>
<dependencies>
<!-- Undertow容器 -->
<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>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>${undertow.version}</version>
</dependency>
</dependencies>
请根据实际需求选择适合的容器,并将相应的配置添加到Spring Boot应用程序的配置文件(application.properties或application.yml)或pom.xml中。