我正在测试Spring应用程序的控制器。我希望将资源映射到路径,这应该是我的API的一部分。我的控制器对路径非常具体:
@Controller
public class DefaultController
{
@RequestMapping("${web-interface}")
public String main()
{
return "index.html";
}
}这里‘web-接口’是一个属性,在application.yml文件中指定。
spring:
datasource:
url: jdbc:mysql://localhost:3306/search-engine
username: landsreyk
password: 12345678
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
show-sql: false
hibernate:
ddl-auto: none
web-interface: /admin预期行为:
路径:localhost:8080/管理映射到index.html资源
根路径: localhost:8080/映射为空,即404错误。
实际行为:
路径:“/admin”映射到index.html
路径:'/‘也映射到index.html
但是为什么呢?我不应该只看“白字错误页”吗?没有控制器,它将根路径映射到index.html文件。这没有任何意义。
顺便说一句,这是我的项目结构。

解决方案:
将index.html重命名为任何其他名称,如main.html和根路径'/‘将不再映射到该资源。
发布于 2021-12-04 10:54:42
根路径“/”默认情况下的映射到index.html。它是所有语言和框架的标准。index.html是指作为应用程序的入口点。
发布于 2021-12-04 11:11:39
它是每个入口点的默认行为。DefaultController实现默认行为,这就是为什么您调用“/或"/root"”并不重要。欲了解更多信息:https://docs.spring.io/spring-boot/docs/2.6.1/reference/htmlsingle/#web.servlet.spring-mvc.welcome-page
https://stackoverflow.com/questions/70224916
复制相似问题