访问控制器映射的 URL 差异

时间:2020-12-25 11:33:27

标签: java spring spring-boot spring-mvc

我正在尝试学习 Spring Framework,在通过 Spring MVCSpring Boot REST 创建一个虚拟项目时,我注意到 Tomcat 响应方式的以下差异您在浏览器中的 URL

Spring Boot

http://localhost:8080/

Redirects to the Controller that is mapped to "/"

http://localhost:8080/springbootrestapp

where "springbootrestapp" is the project name, it gives HTTP 404 resource not found error

Spring MVC

http://localhost:8080/

Opens the Tomcat Home page

http://localhost:8080/springmvc

where "springmvc" is the project name, it redirects to the Controller that is mapped to "/"

我的疑问是为什么在 Spring Boot 的情况下只输入 localhost:8080 重定向到映射到 controller"/" 而在 Spring MVC 的情况下(没有 Spring启动配置)它需要 http://localhost:8080/<projectname>/ 重定向到 controller 映射到 "/"

2 个答案:

答案 0 :(得分:0)

第一个选项非常基本:我们只需删除 /ROOT/ 中的 $CATALINA_HOME\webapps 文件夹,重命名我们的 springbootrestapp.war(或 {{ 1}}) 到 springmvc.war,并部署它。

我们的应用程序现在可以在 http://localhost:8080/ 使用。

有不同的解决方案可以不更改文件名。 You can review the recommendations here.

答案 1 :(得分:0)

我遇到了同样的问题在我的情况下,在运行我的 Spring-MVC 应用程序之前,我启动了 tomcat 服务器,我没有停止,所以每当我访问 localhost:8080/ 时,它都会将我重定向到 Tomcat 客户端。

相关问题