1、前言

SpringBoot的web环境中默认使用tomcat作为内置服务器,其实SpringBoot提供了另外2中内置服务器供我们选择,我们可以很方便的进行切换。

Jetty: Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语编写的,它的API以一组JAR包的形式发布。

Undertow: 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式服务器

2、操作步骤

  1. 在spring-boot-starter-web排除tomcat

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
     	<!--排除tomcat的starter依赖-->   
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    
  2. 导入其他容器的starter

    <!--导入jetty容器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>