28

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.

datree
  • 483
  • 3
  • 7
  • 14
  • By any chance do you have the reference of spring-boot-starter-web? Because that pulls the tomcat automatically as a dependency. – Nitin Arora Aug 08 '15 at 22:44
  • No I don't use spring-boot-starter-web. That's why I added spring-web instead. – datree Aug 08 '15 at 23:33
  • What's the type of your application? Is it a Web or Command line or Scheduler based application? Also, can you share the Main Application class and pom file for reference? I have a sample application which is a Command line application and does not pull tomcat as a dependency. – Nitin Arora Aug 08 '15 at 23:39

3 Answers3

48

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);
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • Thank you. I addapted it to `SpringApplication app = new SpringApplication(MyApp.class); app.setWebApplicationType(WebApplicationType.NONE); app.run(args).close();` and it works perfectly! – aalku Oct 18 '22 at 18:37
22

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.

Tony
  • 5,972
  • 2
  • 39
  • 58
1

You can just close the app according to https://spring.io/guides/gs/async-method/. Although this still stars Tomcat, but will stop the app at the end without keeping the tread running.

SpringApplication.run(MyApp.class, args).close();
barryku
  • 2,496
  • 25
  • 16
  • This closes the application context. You shouldn't close it unless you want to stop the whole application or you really know what you are doing. – aalku Oct 18 '22 at 18:35