我为什么在Tomcat 8.5.85的*.war部署中收到404错误?

huangapple go评论113阅读模式
英文:

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(&quot;/spring-mvc&quot;)
public class HomeController {
@GetMapping(&quot;/index&quot;)
public String homePage() {
return &quot;&lt;p style=&#39;color: green;&#39;&gt;Hello, World&lt;/p&gt;&quot;;
}
}

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;3.0.2&lt;/version&gt;
&lt;relativePath/&gt; 
&lt;/parent&gt;
&lt;groupId&gt;com.visionthing&lt;/groupId&gt;
&lt;artifactId&gt;spring-mvc&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;spring-mvc&lt;/name&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;properties&gt;
&lt;java.version&gt;19&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
&lt;version&gt;6.0.4&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;3.0.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;jstl&lt;/artifactId&gt;
&lt;version&gt;1.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;finalName&gt;spring-mvc&lt;/finalName&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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
│&#160;&#160; └── com
│&#160;&#160;     └── visionthing
│&#160;&#160;         └── springmvc
│&#160;&#160;             ├── HomeController.java
│&#160;&#160;             └── SpringMvcApplication.java
├── resources
│&#160;&#160; └── application.properties
└── webapp
└── WEB-INF
└── jsp

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.7.8&lt;/version&gt;
&lt;!--&lt;version&gt;3.0.2&lt;/version&gt;--&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.visionthing&lt;/groupId&gt;
&lt;artifactId&gt;spring-mvc&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;name&gt;spring-mvc&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;19&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;finalName&gt;spring-mvc&lt;/finalName&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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(&quot;/spring-mvc&quot;)
public class HomeController {
@GetMapping(&quot;/index&quot;)
public String homePage() {
return &quot;&lt;p style=&#39;color: green;&#39;&gt;Hello, World&lt;/p&gt;&quot;;
}
}

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

  1. Change pom.xml,
  • spring boot version to 3.0.2
  • remove compiler configuration : source , target 1.8
  1. Change Tomcat
  • Use Tomcat 10.x

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;!--&lt;version&gt;2.7.8&lt;/version&gt;--&gt;
&lt;version&gt;3.0.2&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.visionthing&lt;/groupId&gt;
&lt;artifactId&gt;spring-mvc&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;name&gt;spring-mvc&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;19&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;finalName&gt;spring-mvc&lt;/finalName&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

Tomcat 10.x

huangapple
  • 本文由 发表于 2023年2月14日 19:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定