[英]Spring boot - Rest Call client without embedded tomcat
I have been trying to figure out an issue with spring boot and as i am new to spring I thought of getting some help here.我一直在试图找出 Spring Boot 的一个问题,因为我是 Spring 的新手,所以我想在这里获得一些帮助。
I have a spring boot based java application which runs as a daemon and makes some GET request to a remote server.我有一个基于 Spring Boot 的 java 应用程序,它作为守护进程运行,并向远程服务器发出一些 GET 请求。 (Acts only as a client).
(仅作为客户)。
But my spring boot application internally starts an embedded tomcat container.但是我的 spring boot 应用程序在内部启动了一个嵌入式 tomcat 容器。 My understanding is that if the java app acts as a server, it would need tomcat.
我的理解是,如果 java 应用程序充当服务器,则需要 tomcat。 But my application being only a consumer of remote machine's GET APIs, why would it need an embedded tomcat ?
但是我的应用程序只是远程机器的 GET API 的使用者,为什么它需要一个嵌入式 tomcat ?
In my pom file I have specified spring-boot-starter-web, on assumption that it is needed for even making GET calls.在我的 pom 文件中,我指定了 spring-boot-starter-web,假设它甚至需要进行 GET 调用。
But after doing some research on disabling embedded tomcat, I found a solution.但是在对禁用嵌入式 tomcat 做了一些研究之后,我找到了一个解决方案。
To make following changes,要进行以下更改,
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
& in application.yml & 在 application.yml 中
spring:
main:
web-environment: false
With the application.yml changes, my jar is not even getting started, aborts directly, without even logging anything in logback logs.随着 application.yml 的变化,我的 jar 甚至没有开始,直接中止,甚至没有在 logback 日志中记录任何内容。
Now, if i remove the application.yml change, my jar starts (only with first change in @SpringBootApplication anno.) but goes into some exception.现在,如果我删除 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.
My Doubts here are,我的疑问是,
1) Is tomcat, be it standalone or embedded, really needed for a application which just makes GET API calls to remote machine ? 1)tomcat,无论是独立的还是嵌入式的,真的需要一个只对远程机器进行GET API调用的应用程序吗?
2) How do i overcome this exception and safely remove the embedded tomcat and still perform the GET API calls ? 2) 我如何克服这个异常并安全地删除嵌入式 tomcat 并仍然执行 GET API 调用?
You seem to be on completely the wrong track here, starting from a web application template and then trying to turn off the web application aspect.您似乎完全走错了路,从 Web 应用程序模板开始,然后尝试关闭 Web 应用程序方面。
Far better to start from a regular commandline client template and go from there, as detailed in the relevant Spring Guide .最好从常规命令行客户端模板开始,然后从那里开始,如相关 Spring 指南中所述。
Basically the application reduces to基本上应用程序减少到
@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());
};
}
}
And the pom to和 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>
I had this problem.我有这个问题。 All I wanted was to have a Client making REST requests.
我想要的只是让一个客户端发出 REST 请求。 Unfortunately I had a dependency which was embedding Jetty, and Jetty was always started.
不幸的是,我有一个嵌入 Jetty 的依赖项,而 Jetty 总是被启动。
In order to disable Jetty all I needed to do was to add in applications.properties the following entry:为了禁用 Jetty,我需要做的就是在 applications.properties 中添加以下条目:
spring.main.web-application-type=none
That fixed it.那修复了它。
Here is the most simple solution for me, make spring boot application just a restful api consumer.这是对我来说最简单的解决方案,让 spring boot 应用程序只是一个安静的 api 消费者。
Replace the dependence替换依赖
implementation("org.springframework.boot:spring-boot-starter-web")
with和
implementation("org.springframework.boot:spring-boot-starter-json")
RestTemplate
and jackson
are available in the project without embedded tomcat. RestTemplate
和jackson
在项目中可用,没有内嵌tomcat。
Answering your questions:回答您的问题:
1) embedded by defaut - not needed for clients HTTP requests; 1) 由默认嵌入 - 客户端 HTTP 请求不需要;
2) You can use CommandLineRunner for spring boot applications without any web: 2) 您可以在没有任何 Web 的情况下将 CommandLineRunner 用于 spring boot 应用程序:
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
@Override
public void run(String... args) {
// TODO: start you thread here to execute client HTTP REST requests (or any other job you need)
}
}
This will disable web completelly - no issues with manual miss-configuration.这将完全禁用网络 - 没有手动错误配置的问题。
Here is some docs: http://www.baeldung.com/spring-boot-console-app这是一些文档: http : //www.baeldung.com/spring-boot-console-app
You also need replase spring-boot-starter-web dependency with spring-boot-starter:您还需要使用 spring-boot-starter 替换 spring-boot-starter-web 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
From your question, i assume you want your application to keep running in background and it makes some get calls in it's life cycle.根据您的问题,我假设您希望您的应用程序继续在后台运行,并在其生命周期中进行一些 get 调用。 If that's the case, then
如果是这样,那么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.