How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate

孤者浪人 提交于 2019-12-03 11:04:08

问题


I want to use RestTemplate/TestRestTemplate by including the artifact in a SpringBoot application

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>

But this automatically starts Tomcat or Jetty. Is there a way to turn it off, or by not including the above artifact. TestRestTemplate is in the boot artifact, but not the base RestTemplate.


回答1:


Spring Boot is not going to start a web container if it's not present. spring-web does not provide any embedded container. You may want to analyse the dependencies of your project (try mvn dependency:tree).

If you want to make sure a web server is not started in your spring boot application, you can set the following configuration key

spring.main.web-application-type=none

Or you can use the SpringApplicationBuilder

new SpringApplicationBuilder(YourApp.class)
        .web(WebApplicationType.NONE).run(args);



回答2:


Since Spring Boot 2.0.0 this property is deprecated and following is the new way:

spring.main.web-application-type=none

This change is because Spring Boot the support for reactive server.



来源:https://stackoverflow.com/questions/31897165/how-to-prevent-auto-start-of-tomcat-jetty-in-spring-boot-when-i-only-want-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!