# 切换内置 Web 服务器

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

查看依赖的路径:

spring-boot-autoconfigure / org / autoconfigure / web / embedded

其中的文件:

image-20230910212613275

这四个文件就是对应的 web 服务器的工厂

第一个:Jetty 服务器

第二个:Netty 服务器

第三个:tomcat 服务器

第四个:Undertow 服务器

排除默认的 tomcat 启动 服务器,改为 jetty 服务器 启动

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除 tomcat 依赖 -->
            <exclusions>
                <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

启动后查看是使用的 jetty 启动 的服务器

image-20230910214302727