春季启动-没有嵌入式tomcat的Rest Call客户端

Dee*_*mar 5 java spring tomcat spring-boot embedded-tomcat-8

我一直在尝试找出Spring Boot的问题,由于我是Spring的新手,所以我想在这里获得一些帮助。

我有一个基于Spring Boot的Java应用程序,它作为守护程序运行,并向远程服务器发出一些GET请求。(仅充当客户)。

但是我的Spring Boot应用程序在内部启动了嵌入式tomcat容器。我的理解是,如果Java应用程序充当服务器,它将需要tomcat。但是我的应用程序只是远程计算机GET API的使用者,为什么它需要嵌入式tomcat?

在我的pom文件中,我假设甚至进行GET调用都需要使用spring-boot-starter-web。

但是在对禁用嵌入式tomcat进行了一些研究之后,我找到了一个解决方案。

要进行以下更改,

@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class, 
WebMvcAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)

&in application.yml

spring:
   main:
      web-environment: false
Run Code Online (Sandbox Code Playgroud)

随着application.yml的更改,我的jar甚至无法启动,直接中止,甚至没有在logback日志中记录任何内容。

现在,如果我删除application.yml更改,则我的jar开始运行(仅在@SpringBootApplication anno中进行第一次更改),但是会出现一些异常。

 [main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
Run Code Online (Sandbox Code Playgroud)

我的疑问是

1)对于只对远程计算机进行GET API调用的应用程序,确实需要tomcat(无论是独立的还是嵌入式的)?

2)我如何克服此异常并安全地删除嵌入式tomcat并仍然执行GET API调用?

jwe*_*ing 12

您在这里似乎完全走错了路,从 Web 应用程序模板开始,然后尝试关闭 Web 应用程序方面。

最好从常规命令行客户端模板开始,然后从那里开始,如相关 Spring 指南中所述

基本上应用程序减少到

@SpringBootApplication
public class Application {

private static final Logger log = LoggerFactory.getLogger(Application.class);

public static void main(String args[]) {
    SpringApplication.run(Application.class);
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
}

@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
    return args -> {
        Quote quote = restTemplate.getForObject(
                "http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
        log.info(quote.toString());
    };
}
}
Run Code Online (Sandbox Code Playgroud)

和 pom 到

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)


Buc*_*oka 5

我有这个问题。我想要的只是让一个客户端发出 REST 请求。不幸的是,我有一个嵌入 Jetty 的依赖项,而 Jetty 总是被启动。

为了禁用 Jetty,我需要做的就是在 applications.properties 中添加以下条目:

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

那修复了它。