如果在springboot的版本2.0以上且用的是HandlerInterceptor拦截器,那么所有的静态资源请求都会被拦截,从而造成404.
所以要在拦截器中排除静态资源路径。
@Configuration
public class StaticConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new StaticInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/static/**");
// super.addInterceptors(registry);
}
}
因为我配置了
static-path-pattern: /static/**
,我的静态资源访问是需要加上static的,
例如:http://localhost:8080/static/css/test.css
如果没配置的话,springboot会自己映射到static
例如:http://localhost:8080/css/test.css