spring-boot-starter-tomcat vs spring-boot-starter-web
Asked Answered
S

2

32

I'm trying to learn Spring boot and I notice there are two options.

  1. spring-boot-starter-web - which according to the docs gives support for full-stack web development, including Tomcat and web-mvc

  2. spring-boot-starter-tomcat

Since #1 supports Tomcat why would one want to use #2?

What are the differences?

Thanks

Soerabaja answered 29/10, 2015 at 16:32 Comment(0)
F
41

Since #1 supports Tomcat why would one want to use #2?

spring-boot-starter-web contains spring-boot-starter-tomcat. spring-boot-starter-tomcat could potentially be used on its own if spring mvc isn't needed (contained in spring-boot-starter-web).

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

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 an embdedded tomcat server:

core
el
logging
websocket

What if you want to use spring mvc without the embedded 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>
Fazio answered 29/10, 2015 at 16:36 Comment(2)
What if I want the embedded tomcat but with a different tomcat version? I add a different version of starter tomcat separately by excluding as you have mentioned but still does not work.Filiano
@Filiano for Gradle, add the following to build.gradle: ext { set('tomcat.version', "9.0.62") } ... not sure for Maven but probably straightforward to figure out.Carencarena
I
7

Well a simple answer is that not all web applications are SpringMVC applications. For example if you wish to use JaxRS instead perhaps you have client applications that use RestTemplate and you like how they interact it doesn't mean you can't 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 application in spring boot using spring-boot-starter-tomcat

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

It's also important to remember that tomcat is not the only option for an embedded servlet container in spring boot. It's also easy to get started using jetty. And having spring-boot-starter-tomcat makes it easy to exclude all as one module while if they were all just part of spring-web it would be more work to exclude the tomcat libraries to bring in 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 (easily?)

Iaverne answered 29/10, 2015 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.