java - Spring boot - 没有嵌入式tomcat的Rest Call客户端

标签 java spring spring-boot tomcat embedded-tomcat-8

我一直在尝试找出 spring boot 的问题,因为我是 spring 的新手,所以我想在这里获得一些帮助。

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

但是我的 spring boot 应用程序在内部启动了一个嵌入式 tomcat 容器。 我的理解是,如果 java 应用程序充当服务器,则需要 tomcat。但是我的应用程序只是远程机器的 GET API 的使用者,为什么它需要一个嵌入式 tomcat?

在我的 pom 文件中,我指定了 spring-boot-starter-web, 假设即使进行 GET 调用也需要它。

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

要进行以下更改,

@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class, 
WebMvcAutoConfiguration.class})

& 在 application.yml 中

spring:
   main:
      web-environment: false

随着 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.

我的疑问是,

1) tomcat,无论是独立的还是嵌入式的,对于只对远程机器进行 GET API 调用的应用程序来说真的需要吗?

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

最佳答案

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

最好从常规命令行客户端模板开始,然后从那里开始,详见 relevant Spring Guide .

基本上应用程序减少到

@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());
    };
}
}

和 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>

关于java - Spring boot - 没有嵌入式tomcat的Rest Call客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51494829/

相关文章:

java - 无法使用构造函数创建对象

java - Jsp 包含不工作 : file not found, 状态 500

Java 在单元格数量减半后开始读取 null

mysql - Java企业项目中的封装级别

spring - 如何在 intellij idea 中更快地调试 Spring Boot gradle 项目?

java - Spring Boot CommandLineRunner 异常处理

java - 如何编写 dockerfile 以在包含 jdk 的现有 spring boot 项目上添加 maven 构建

spring-boot - 用于执行gradle bootBuildImage命令的Docker镜像

java - SpringBoot2 配置 JOOQ MariaDB : "required a bean of type ' javax. sql.DataSource' 找不到”

java - 检查 joda.time.Interval 是否正好跨越 1 个日历周的更好方法(考虑夏令时等)