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
標注的類。
出現Whitelabel Error Page問題的情況:
- Controller等組件不在SpringBoot的掃描路徑中。
- 沒有在
application.properties
文件中指定server.servlet.context-path
。
參考: