Spring-boot-starter-tomcat vs spring-boot-starter-web

I am trying to learn Spring boot, and I notice that there are two options.

  • spring-boot-starter-web - which, according to the docs, provides full-stack web development support, including Tomcat and web-mvc

  • spring-boot-starter-cat

Since # 1 supports Tomcat, why use # 2?

What are the differences?

thanks

+5
source share
2 answers

Since # 1 supports Tomcat, why use # 2?

spring-boot-starter-web contains spring-boot-starter-tomcat . spring-boot-starter-tomcat can potentially be used on its own if spring mvc is not required (contained in spring-boot-starter-web ).

Here is the spring-boot-starter-web dependency hierarchy:

enter image description here

What are the differences?

spring-boot-starter-web contains spring web dependencies (including spring-boot-starter-tomcat ):

spring-boot-starter
jackson
spring-core
spring-mvc
spring-boot-starter-tomcat

spring-boot-starter-tomcat contains everything related to the tomcat embedded server:

core
el
logging
websocket

What if you want to use spring mvc without the built-in tomcat server?

Just exclude it from the dependency:

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> 
+6
source

Well, the simple answer is that not all web applications are SpringMVC applications. For example, if you want to use JaxRS, perhaps you have client applications that use RestTemplate and you like the way they interact, this does not mean that you cannot use spring boot or embedded tomcat

Here is an example application that uses spring-boot-starter-tomcat but not spring-boot-starter-web

Simple jersey app in spring using spring-boot-starter-tomcat

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-jersey

It is also important to remember that tomcat is not the only option for the built-in servlet container in spring boot. It is also easy to start using the pier. And with spring-boot-starter-tomcat , you can easily exclude everything as one module, and if they were just part of spring -web, it would be more work to exclude tomcat libraries for entering spring-boot-starter-jersey instead

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> 

I copied this code from another SO question here.

How to configure Jetty in spring-boot (easy?)

+2
source

Source: https://habr.com/ru/post/1234809/


All Articles