大家好,我是 程序員碼遞夫。
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();}}
修改過后,我的小美眉出來啦。
希望對你有幫助.