大家好,我是 程序员码递夫。
SpringBoot请求静态资源时,request.getServletPath() 返回error, 明明我的目录文件是存在的怎么就报错了呢?
如我请求 http://127.0.0.1:9090/Hanfu/upload/1647161536390.png
通常是因为请求的资源没有被正确处理或定位引起的。
检查一下你的资源注入配置,是否写对了,可参考如下内容:
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/static/upload/");registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");super.addResourceHandlers(registry);}@Overrideprotected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(jwtInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**").excludePathPatterns("/static/**");super.addInterceptors(registry);}@Beanpublic JwtInterceptor jwtInterceptor() {return new JwtInterceptor();}}
修改过后,我的小美眉出来啦。
希望对你有帮助.