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。
参考: