英文:
why am i getting a 404 error at Tomcat 8.5.85 *.war deployment
问题
我尝试在Tomcat服务器上部署一个简单的Spring Boot项目。
在Intellij IDEA中本地运行一切正常。
这是我的RestController:
@RestController
@RequestMapping("/spring-mvc")
public class HomeController {
@GetMapping("/index")
public String homePage() {
return "<p style='color: green'>Hello, World</p>";
}
}
这是我的主类:
@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);
}
}
这是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>
我通过Maven命令行导出了WAR文件,并将其移动到Tomcat安装目录的/webapps文件夹中。
Tomcat服务器正在运行OpenJDK 19,与我的本地项目相同。
当打开Tomcat Web管理界面时,文件夹显示出来,点击它会重定向到http://localhost:8081/spring-mvc/,该站点显示Tomcat的404错误页面,以及http://localhost:8081/spring-mvc/index 和 http://localhost:8081/spring-mvc/spring-mvc/index。
我原本期望在http://localhost:8081/spring-mvc/index上看到绿色的“Hello, World”。
我是否忘记了什么?
谢谢。
英文:
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
答案1
得分: 1
你需要将 Main.class
添加到 application.sources(HomeController.class)
,而不是 HomeController.class
。像这样:application.sources(Main.class)
。
英文:
you need to add Main.class
to application.sources(HomeController.class)
instead of HomeController.class
. Like this:
application.sources(Main.class)
.
答案2
得分: 0
这是您提供的代码和配置的翻译:
项目目录
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── visionthing
│ ├── HomeController.java
│ └── SpringMvcApplication.java
├── resources
│ └── application.properties
└── webapp
└── WEB-INF
└── jsp
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>2.7.8</version>
<!--<version>3.0.2</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.visionthing</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-mvc</name>
<description>Demo project for Spring Boot</description>
<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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
SpringMvcApplication.java
package com.visionthing.springmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringMvcApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringMvcApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringMvcApplication.class, args);
}
}
HomeController.java
package com.visionthing.springmvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/spring-mvc")
public class HomeController {
@GetMapping("/index")
public String homePage() {
return "<p style='color: green'>Hello, World</p>";
}
}
application.properties (可选)
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
打包
mvn clean package
将spring-mvc.war文件放到tomcat/webapps目录中
启动Tomcat
测试
命令行测试:curl http://localhost:8081/spring-mvc/spring-mvc/index
浏览器测试打开:http://localhost:8081/spring-mvc/spring-mvc/index
结论
为什么要将Spring Boot升级到2.7.8?
因为您可以测试相同的项目和相同的代码,只需更改pom.xml中的Spring Boot版本为3.0.2,然后重新打包war,您将再次获得404错误。
Spring Boot 3.x需要JDK 17,但您的pom.xml将源和目标版本设置为1.8
。
Spring Boot 3.x需要Tomcat 10.x,但您使用的是Tomcat 8.x。
因此,我只能提供一个可以在您的限制下运行的最小示例项目,其中源和目标版本设置为1.8。
此外,我在pom.xml中删除了不相关的依赖项。
英文:
mini spring boot war for spring boot 2.7.8
Project Directory
├── pom.xml
└── src
└── main
├── java
│   └── com
│   └── visionthing
│   └── springmvc
│   ├── HomeController.java
│   └── SpringMvcApplication.java
├── resources
│   └── application.properties
└── webapp
└── WEB-INF
└── jsp
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>2.7.8</version>
<!--<version>3.0.2</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.visionthing</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-mvc</name>
<description>Demo project for Spring Boot</description>
<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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
SpringMvcApplication.java
package com.visionthing.springmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringMvcApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringMvcApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringMvcApplication.class, args);
}
}
HomeController.java
package com.visionthing.springmvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/spring-mvc")
public class HomeController {
@GetMapping("/index")
public String homePage() {
return "<p style='color: green;'>Hello, World</p>";
}
}
application.properties (optional)
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
package
mvn clean 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
- Change
pom.xml
,
- spring boot version to 3.0.2
- remove compiler configuration : source , target 1.8
- Change Tomcat
- Use Tomcat 10.x
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>2.7.8</version>-->
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.visionthing</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-mvc</name>
<description>Demo project for Spring Boot</description>
<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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
</plugin>
</plugins>
</build>
</project>
Tomcat 10.x
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论