我正在測驗 Spring Boot 應用程式的控制器。我想將資源映射到路徑,該路徑應該是我的 API 的一部分。我的控制器對路徑非常具體:
@Controller
public class DefaultController
{
@RequestMapping("${web-interface}")
public String main()
{
return "index.html";
}
}
這里的 'web-interface' 是一個屬性,在 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/admin 映射到 index.html 資源
根路徑: localhost:8080/ 映射為空,即 404 錯誤。
實際行為:
路徑:'/admin' 映射到 index.html
路徑:'/' 也映射到 index.html
但為什么?我不應該只看到“白標錯誤頁面”。沒有將根路徑映射到 index.html 檔案的控制器。這沒有任何意義。
順便說一下,這是我的專案結構。

解決方案:
將 index.html 重命名為任何其他名稱,例如 main.html,根路徑“/”將不再映射到該資源。
uj5u.com熱心網友回復:
根路徑“/”默認映射到 index.html。它是所有語言和框架的標準。index.html是應用程式的入口點
uj5u.com熱心網友回復:
這是每個入口點的默認行為。該DefaultController工具的默認行為,這就是為什么,如果你把它并不重要“/”或“/根”。更多資訊:https : //docs.spring.io/spring-boot/docs/2.6.1/reference/htmlsingle/#web.servlet.spring-mvc.welcome-page
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373298.html
