编辑:通过使主 Startup 类扩展 SpringBootServletInitializer 解决了这个问题。
我正在尝试使用 webapp-runner 和 .war 打包通过 Maven 部署一个 Spring MVC 项目。
当通过 IntelliJ 的运行配置运行时,它在本地 Tomcat 安装上运行良好,但当我尝试运行 war 包时,它总是返回未找到。
我使用以下命令运行它:
java -jar target/dependency/webapp-runner.jar target/*.war
这是我的项目结构:
An important note to make here is that if I add a random index.html file inside the webapp directory it is mapped correctly and found with a 200 OK.
我对 Spring 技术还很陌生,所以如果你能帮助我解决这个应用程序的架构问题,以便请求映射在打包后工作,我将非常感激。
这是我的家庭 Controller :
@Controller
@RequestMapping("/")
public class HomeController {
public String index() {
return "index";
}
}
我应该改变什么?我的 Thymeleaf 模板应该在 webapp 文件夹中吗?为什么Tomcat打包后不能用现在的映射方式定位?
最佳答案
在 war 文件中,URL 上下文是 war 文件的名称。所以在你的情况下它是 http://host:port/slupov-personal/
。
关于java - Spring MVC - 对Tomcat中应用程序的所有请求在war打包后导致错误404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58414043/