当我只想使用RestTemplate时,如何防止在Spring Boot中自动启动tomcat / jetty

我想通过在SpringBoot应用程序中包含工件来使用RestTemplate / TestRestTemplate

 org.springframework spring-web  

但这会自动启动Tomcat或Jetty。 有没有办法将其关闭,或者不包括上述工件。 TestRestTemplate位于引导工件中,但不是基础RestTemplate。

如果不存在,Spring Boot不会启动Web容器。 spring-web不提供任何嵌入式容器。 您可能想要分析项目的依赖关系(尝试mvn dependency:tree )。

如果要确保弹出引导应用程序中未启动Web服务器,可以设置以下配置密钥

 spring.main.web-environment=false 

或者您可以使用SpringApplicationBuilder

 new SpringApplicationBuilder(YourApp.class) .web(false).run(args); 

从Spring Boot 2.0.0开始,不推荐使用此属性,以下是新方法:

 spring.main.web-application-type=none 

这种变化是因为Spring Boot支持反应式服务器。