页面报错
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).No message available
日志报错
WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name ‘dispatcherServlet’
无java异常发生。
发生原因
Spring boot本来是默认使用public下的index页面作为默认首页的,这属于Spring boot的自动配置,本项目中使用到了拦截器,因而使用了配置类生成拦截器,在配置类中继承了WebMvcConfigurerAdapter这个类,但因为此类已被标注过时,所以使用了WebMvcConfigurationSupport类来代替,但这个类的说明中含有其实例会使自动配置失效的描述,因此使得Spring boot的自动配置的静态资源路径失效。
解决办法
WebMvcConfigurerAdapter类只含有对WebMvcConfigurer接口的空实现,因此我们可以直接使配置类实现WebMvcConfigurer接口,不必继承这两个类。
拦截器实现方式略有差别:
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 用于添加拦截规则, 这里假设拦截 /url 后面的全部链接
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/index");
}