Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate

Tags:

I want to use RestTemplate/TestRestTemplate by including the artifact in a SpringBoot application

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

But this automatically starts Tomcat or Jetty. Is there a way to turn it off, or by not including the above artifact. TestRestTemplate is in the boot artifact, but not the base RestTemplate.

like image 531
datree Avatar asked Aug 08 '15 18:08

datree


People also ask

Can we exclude Tomcat in spring boot?

If we want to exclude tomcat from spring boot, we don't need to do much, we just need to add one additional block(<exclusions>) to the Spring Boot dependency. <exclusions> tag is used to make us sure that given server/artifactId is being removed at the time of build.

Do you think you can use Jetty instead of Tomcat in spring boot starter web?

73.11 Use Jetty instead of TomcatYou need to exclude those dependencies and include the Jetty one instead. Spring Boot provides Tomcat and Jetty dependencies bundled together as separate starters to help make this process as easy as possible. Example in Gradle: configurations { compile.

How do I change the default Tomcat in spring boot?

You will need to update pom. xml , add the dependency for spring-boot-starter-jetty . Also, you will need to exclude default added spring-boot-starter-tomcat dependency.


2 Answers

Spring Boot is not going to start a web container if it's not present. spring-web does not provide any embedded container. You may want to analyse the dependencies of your project (try mvn dependency:tree).

If you want to make sure a web server is not started in your spring boot application, you can set the following configuration key

spring.main.web-application-type=none 

Or you can use the SpringApplicationBuilder

new SpringApplicationBuilder(YourApp.class)         .web(WebApplicationType.NONE).run(args); 
like image 159
Stephane Nicoll Avatar answered Sep 17 '22 16:09

Stephane Nicoll


Since Spring Boot 2.0.0 this property is deprecated and following is the new way:

spring.main.web-application-type=none 

This change is because Spring Boot the support for reactive server.

like image 38
Tony Avatar answered Sep 21 '22 16:09

Tony