DavidR
DavidR

Reputation: 6962

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

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

Upvotes: 32

Views: 26571

Answers (2)

ltalhouarne
ltalhouarne

Reputation: 4636

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>

Upvotes: 41

Zergleb
Zergleb

Reputation: 2302

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

Upvotes: 7

Related Questions