Spring Boot替换内置Tomcat容器

spring-boot-starter-web包中默认自带的容器是Tomcat,所以默认情况下启动完成是这样的:
在这里插入图片描述

改成Undertow容器

如果要替换掉内置的Tomcat容器,比如改成Undertow容器,需要先排除spring-boot-starter-web包中的Tomcat依赖,再增加spring-boot-starter-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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

这个方法是切实可行的,也经过验证了,启动后效果如下:
在这里插入图片描述
不过下载依赖包时可能会出现下载失败,我这网不好,就很难下成功,所以一直反复删除重导,再重复的clean和compile,最后才终于可以,所以如果一直没法成功的启用Undertow容器,一定不是你不行,是网不行,坚持一下,多搞搞,最后肯定是能成功的,不能动摇。

改成Jetty容器

改成Jetty容器,也是需要先排除spring-boot-starter-web包中的Tomcat依赖,再增加spring-boot-starter-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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        

启动后效果如下:
在这里插入图片描述

总结

如果真的网不好,下载经常缺这少那的,建议按照以下步骤重复试试
在这里插入图片描述
celan->compile->install,然后启动,这时如果还是没有用到我们替换的容器,就把pom.xml文件里的Undertow或者Jetty容器的依赖删除,重新imort changes,然后再把依赖重新加进来,再次imort changes,乱拳打死老师傅,我就是这样操作成功的,给个建议,希望有用,不然时间浪费在研究导依赖包上,就太伤了,当然网络好的,就不用这么麻烦了。。。

发布了321 篇原创文章 · 获赞 345 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_38106322/article/details/105410498