I’m trying to deploy a simple spring boot project on tomcat server.
Locally via Intellij IDEA everything is working.
This is my RestController:
@RestController
@RequestMapping("/spring-mvc")
public class HomeController {
@GetMapping("/index")
public String homePage() {
return "<p style='color: green;'>Hello, World</p>";
}
}
This is my Main Class
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Main extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Main.class);
}
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
This is the 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.2</version>
<relativePath/>
</parent>
<groupId>com.visionthing</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mvc</name>
<packaging>war</packaging>
<properties>
<java.version>19</java.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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-mvc</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I exported the war via maven cli and moved it to the /webapps folder of my tomcat installation.
tomcat is running openjdk 19 as well as my local project.
When opening tomcat webui manager the folder shows up, clicking on it redirects me to http://localhost:8081/spring-mvc/ this site displays the 404 error page of tomcat.
as well as
http://localhost:8081/spring-mvc/index
http://localhost:8081/spring-mvc/spring-mvc/index
I was expecting to see Hello, World written in green on http://localhost:8081/spring-mvc/index
did i forget something?
thanks in regard.
i’m running linux pop-os based on ubuntu 22.04 LTS
2
Answers
you need to add
Main.class
toapplication.sources(HomeController.class)
instead ofHomeController.class
. Like this:application.sources(Main.class)
.mini spring boot war for spring boot 2.7.8
Project Directory
pom.xml
SpringMvcApplication.java
HomeController.java
application.properties (optional)
package
put spring-mvc.war to tomcat/webapps
start tomcat
test
command line test:
curl http://localhost:8081/spring-mvc/spring-mvc/index
browser test open :
http://localhost:8081/spring-mvc/spring-mvc/index
conclusion
Why I am change spring boot to 2.7.8 ?
Because you can test the same project and the same code , just change pom.xml
Spring boot version to 3.0.2 , and re-package war , you will get 404 again.
Spring Boot 3.x require JDK 17 , but your pom.xml set source and target to version
1.8
.Spring Boot 3.x require Tomcat 10.x , but you use Tomcat 8.x.
So I can only provide a minimum example project that can be executed, for your limitation Tomcat 8.x, source and target is set to 1.8.
In addition, I removed irrelevant dependencies in pom.xml.
UPDATE For Spring Boot 3.0.2
pom.xml
,pom.xml
Tomcat 10.x