Home > Software design >  Spring boot 3.0.1 application not running on tomcat9 external server
Spring boot 3.0.1 application not running on tomcat9 external server

Time:01-05

When using my Eclipse's Tomcat9 server (v9.0.62), my "application" runs normally. Once deployed on my production server however (v9.0.43), 404 is returned. No stacktraces are being logged by Tomcat.

Both servers use OpenJDK17 as JDK. This problem does not happen when using versions of Spring < 6.

Eclipse console output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.1)

2023-01-05T11:07:33.198 01:00  INFO 28395 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 17.0.5 with PID 28395 (/home/-----/demo/target/classes started by ----- in /home/-----/demo)
2023-01-05T11:07:33.207 01:00  INFO 28395 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2023-01-05T11:07:34.391 01:00  INFO 28395 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2023-01-05T11:07:34.403 01:00  INFO 28395 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-05T11:07:34.404 01:00  INFO 28395 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-05T11:07:34.530 01:00  INFO 28395 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-05T11:07:34.532 01:00  INFO 28395 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1263 ms
2023-01-05T11:07:34.889 01:00  INFO 28395 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2023-01-05T11:07:34.895 01:00  INFO 28395 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.316 seconds (process running for 2.652)
2023-01-05T11:07:47.016 01:00  INFO 28395 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-01-05T11:07:47.016 01:00  INFO 28395 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-01-05T11:07:47.017 01:00  INFO 28395 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

When building (maven clean install) the war file, still no issue:

[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ demo ---
[INFO] Deleting /home/-----/demo/target
[INFO] 
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ demo ---
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/-----/demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ demo ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ demo ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:3.3.2:war (default-war) @ demo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo] in [/home/-----/demo/target/demo-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/-----/demo/src/main/webapp]
[INFO] Building war: /home/-----/demo/target/demo-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:3.0.1:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- maven-install-plugin:3.0.1:install (default-install) @ demo ---
[INFO] Installing /home/-----/demo/pom.xml to /home/--/.m2/repository/com/example/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.pom
[INFO] Installing /home/-----/demo/target/demo-0.0.1-SNAPSHOT.war to /home/--/.m2/repository/com/example/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.232 s
[INFO] Finished at: 2023-01-05T11:09:35 01:00
[INFO] ------------------------------------------------------------------------

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
<!--        <tomcat.version>9.0.62</tomcat.version>-->
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Application:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

ServletInitializer:

package com.example.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

}

Controller:

package com.example.demo.ctrl;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {

    @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody String getMain() {
        return "super";
    }
}

Expected (Eclipse's server):

enter image description here

Found (Production server):

enter image description here enter image description here

To pinpoint the problem (as my project is rather large), I reproduced the issue with a simple app based on Spring Initializr.

I attempted tweaking the pom.xml to no avail.

CodePudding user response:

Spring boot 3 requires at least JDK17 and Tomcat 10 due to the package renaming from javax to jakarta.

Tomcat 9 does not work with jakarta packages which were changes introduced in Spring 6/Spring boot 3.

If you want to stay on Tomcat 9 you will need to downgrade to Spring boot version 2.x.x, any Spring boot 3 version will require Tomcat upgrade to version 10.

  • Related