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

Java 当我只想使用RestTemplate时,如何防止在Spring Boot中自动启动tomcat/jetty,java,spring,spring-boot,resttemplate,Java,Spring,Spring Boot,Resttemplate,我想通过在SpringBoot应用程序中包含工件来使用RestTemplate/TestRestTemplate <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> org.springframework 弹簧网 但这会自

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

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

org.springframework
弹簧网

但这会自动启动Tomcat或Jetty。有没有办法关闭它,或者不包括上面的工件。TestRestTemplate位于引导工件中,但不在基本RestTemplate中。

如果web容器不存在,Spring boot将不会启动它<代码>spring web不提供任何嵌入式容器。您可能需要分析项目的依赖关系(请尝试
mvn-dependency:tree

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

spring.main.web-application-type=none
或者您可以使用
SpringApplicationBuilder

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

自Spring Boot 2.0.0以来,此属性已被弃用,以下是新方法:

spring.main.web-application-type=none

这一变化是因为Spring Boot支持

,您可以根据需要关闭应用程序。虽然这仍然是Tomcat的明星,但最终会停止应用程序,而不会保持运行状态

SpringApplication.run(MyApp.class, args).close();

你有spring boot starter网站的参考资料吗?因为这会自动将tomcat作为一个依赖项拉入。不,我不使用SpringBootStarterWeb。这就是我添加SpringWeb的原因。您的应用程序是什么类型的?它是基于Web、命令行还是基于调度器的应用程序?另外,您能否共享主应用程序类和pom文件以供参考?我有一个示例应用程序,它是一个命令行应用程序,并没有将tomcat作为依赖项。