解決SpringBoot專案出現Whitelabel Error Page的問題(下)
阿新 • • 發佈:2020-08-28
3.解決Whitelabel Error Page的問題
# application.properties
server.servlet.context-path=/restful-demo
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages="com.example") public class RestfulDemoApplication { public static void main(String[] args) { SpringApplication.run(RestfulDemoApplication.class, args); } }
@ComponentScan(basePackages="com.example")
:Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.
Whitelabel Error Page的問題就解決了!
當然,@ComponentScan(basePackages="com.example")
不是必須的。
SpringBoot預設會掃描啟動類所在的包及其子包。啟動類是註解@SpringBootApplication
RestfulDemoApplication
。
出現Whitelabel Error Page問題的情況:
- Controller等元件不在SpringBoot的掃描路徑中。
- 沒有在
application.properties
檔案中指定server.servlet.context-path
。
參考: