Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I have a spring boot based java application which runs as a daemon and makes some GET request to a remote server. (Acts only as a client).

But my spring boot application internally starts an embedded tomcat container. My understanding is that if the java app acts as a server, it would need tomcat. But my application being only a consumer of remote machine's GET APIs, why would it need an embedded tomcat ?

In my pom file I have specified spring-boot-starter-web, on assumption that it is needed for even making GET calls.

But after doing some research on disabling embedded tomcat, I found a solution.

To make following changes,

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

& in 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.

Now, if i remove the application.yml change, my jar starts (only with first change in @SpringBootApplication anno.) but goes into some exception.

 [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 ?

2) How do i overcome this exception and safely remove the embedded tomcat and still perform the GET API calls ?

like image 291
Deepak Selvakumar Avatar asked Jul 24 '18 09:07

Deepak Selvakumar


People also ask

Can we run spring boot application without embedded Tomcat?

You can use Spring Boot without embedded Tomcat web server if you don't need it. Just exclude embedded Tomcat from Spring Boot Web Starter (spring-boot-starter-web).

Can I run spring boot without Tomcat server?

However, Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, and more. In this tutorial, we'll look at several different ways to use Spring Boot without a web server.


2 Answers

Here is the most simple solution for me, make spring boot application just a restful api consumer.

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.

like image 174
alijandro Avatar answered Sep 22 '22 04:09

alijandro


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.

Far better to start from a regular commandline client template and go from there, as detailed in the relevant Spring Guide.

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

    <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>
like image 29
jwenting Avatar answered Sep 25 '22 04:09

jwenting