12

According to the Reference API on Using Tomcat 8 and this Deploy Spring Boot Applications tutorial it should be possible to use Tomcat 8 with Spring Boot, which uses Tomcat 7 as default.

For some reason it is not working for me. What am I doing wrong?

pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<properties>
    <tomcat.version>8.0.3</tomcat.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
</dependencies>

SampleController.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

Output is Starting Servlet Engine: Apache Tomcat/7.0.52

 :: Spring Boot ::        (v1.0.2.RELEASE)

2014-06-09 13:55:27.688  INFO 5744 --- [           main] SampleController                         : Starting SampleController on CI01081252 with PID 5744 (started by TECBMEPI in D:\projetos\teclogica\testes automatizados\exemplo int db\workspace\spring-boot-tomcat8-maven)
2014-06-09 13:55:27.730  INFO 5744 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33b45e9a: startup date [Mon Jun 09 13:55:27 BRT 2014]; root of context hierarchy
2014-06-09 13:55:28.869  INFO 5744 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8081
2014-06-09 13:55:29.117  INFO 5744 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-06-09 13:55:29.118  INFO 5744 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-06-09 13:55:29.226  INFO 5744 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-06-09 13:55:29.226  INFO 5744 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1498 ms
2014-06-09 13:55:29.656  INFO 5744 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-06-09 13:55:29.658  INFO 5744 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-06-09 13:55:29.946  INFO 5744 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-06-09 13:55:30.035  INFO 5744 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String SampleController.home()
2014-06-09 13:55:30.059  INFO 5744 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-06-09 13:55:30.059  INFO 5744 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-06-09 13:55:30.236  INFO 5744 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-06-09 13:55:30.257  INFO 5744 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081/http
2014-06-09 13:55:30.259  INFO 5744 --- [           main] SampleController                         : Started SampleController in 2.933 seconds (JVM running for 3.318)

I have already tried this newer RC version of Spring boot, but still Tomcat 7 is loaded instead of 8.

Also adding <java.version>1.7</java.version> property didnt work.

Tried "application.properties" as well. Port is set as specified on start-up, but version of Tomcat remains the default 7.

application.properties

server.port=8081
tomcat.version=8.0.3
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165

4 Answers4

21

When using Spring Boot overriding tomcat with tomcat.version as a property will only work if you use spring-boot-starter-parent as a parent for your project.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.0.M2</version>
</parent>

(change the version to the one you like).

<properties>
    <tomcat.version>8.0.8</tomcat.version>
</properties>

If you don't want to use this as a parent you will have to use a <dependencyManagement> section in your pom.xml to override all the spring versions and leave the properties as above.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-el</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-websocket</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>

</dependencyManagement>

Either way will work but extending the parent is the easiest I would say, however this isn't always possible.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
1

Did you try overriding tomcat.version in your application.properties? Specifically, setting it to 8.0.3 makes spring boot run 8.0.3 instead of whatever the default is.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • Thhanks for the answer but I tried here and it didnt work. As the tutorial says `Smaller things, like which port the server runs on`. Also, the documentation do not point to the properties file, but only for the classpath dependencies. – Evandro Pomatti Jun 09 '14 at 16:18
  • Is application.properties at the root of your classpath? It works fine for me. – hd1 Jun 09 '14 at 16:35
  • Yes, I also set the port as 8081 to make sure the file is read. [This tutorial](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-tomcat8-jsp) also uses Tomcat 8, and does not have that property set on the properties file, only in the pom.xml. – Evandro Pomatti Jun 09 '14 at 16:46
  • That will restrict you to using maven's boot plugin to run the application, using the method I suggest lets you use java -jar – hd1 Jun 09 '14 at 18:05
  • application.properties is only used at runtime, when it's too late because the Tomcat jars are already on the classpath. So this answer is incorrect. The correct answer is configuraing your build tool (Maven's pom.xml). – Francois Marot Oct 09 '20 at 09:25
0

Here is Official document.

Without Starter Parent:

<project>
    <properties>
        <tomcat.version>8.0.3</tomcat.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- things you wanna override  -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>${tomcat.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-el</artifactId>
                <version>${tomcat.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-websocket</artifactId>
                <version>${tomcat.version}</version>
                <scope>test</scope>
            </dependency>

            <!-- pom -->
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.4.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-juli</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>
</project>
Jess Chen
  • 3,136
  • 1
  • 26
  • 35
-1

I can answer this question the same way I answered it in another thread. A key piece of information that I think some people are unaware of that Spring Boot is only a mock of an actually Java Servlet container like Jetty, Glassfish, Tomcat, etc. You as the developer have to choose which you will use.

Spring Boot is decent but of course has its downfalls and was really only created by the spring.io team to make their tutorials look that much nicer in an attempt to compete with other 'Get up and running in seconds' technologies.

Community
  • 1
  • 1
aaiezza
  • 1,297
  • 3
  • 11
  • 21