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

dat*_*ree 21 java spring resttemplate spring-boot

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

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*oll 40

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

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

spring.main.web-application-type=none
Run Code Online (Sandbox Code Playgroud)

或者你可以使用 SpringApplicationBuilder

new SpringApplicationBuilder(YourApp.class)
        .web(WebApplicationType.NONE).run(args);
Run Code Online (Sandbox Code Playgroud)


Ton*_*ony 19

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

spring.main.web-application-type=none
Run Code Online (Sandbox Code Playgroud)

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