前言:这是我自己搭建的框架,用于以后的项目开发,在今天的访问本地文件上被WebMvcConfigurationSupport 坑的惨兮兮,所以做个记录。
首先直接上代码,上传文件到${web.upload-path}下 (上传博客一大堆,,可自行查看)
web:
# 本地文件存放地址
upload-path: D:/upload/
# 修改默认静态文件访问地址
spring:
resources:
static-locations: file:${web.upload-path}
mvc:
# 访问地址
static-path-pattern: /img/**
这样就通过http://localhost:8080/zm/img/timg.jpg访问到D/upload下的图片了
先别开心,走到这一步可是花了我一天的时间。在此之前我用拦截器拦截了一些项目路由
@Configuration
public class MvcInterceptorConfig extends WebMvcConfigurationSupport {
@Resource
private WebTokenAspect webTokenAspect;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 用于添加拦截规则,/**表示拦截所有请求
// excludePathPatterns 用户排除拦截
registry.addInterceptor(webTokenAspect).addPathPatterns("/user/**").excludePathPatterns("/account/**");
}
}
本以为上面的设置好了就可以看了,结果一份访问我真的泪牛满面
这就是前后端报的错,一直是这样,问了朋友查了很多文档发现没有写错,首先想的问题是为什么是一个请求路由发送到控制器,所以导致报错。
后来改了还是一样的报错,只好注释完所有的项目配置检查,才发现是拦截器的锅。解决:
解决:
只用修改将 extends WebMvcConfigurationSupport 改成 implements WebMvcConfigurer
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Resource
private WebTokenAspect webTokenAspect;
/**
* 拦截页面
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 用于添加拦截规则,/**表示拦截所有请求
// excludePathPatterns 用户排除拦截
registry.addInterceptor(webTokenAspect).addPathPatterns("/user/**").excludePathPatterns("/account/**");
}
}
原因:
使用WebMvcConfigurationSupport时WebMvc自动化配置就会失效
详细链接:https://www.cnblogs.com/hellohero55/p/12072465.html
WebMvcConfigurer 的方法
详细链接:https://blog.youkuaiyun.com/qq_41506826/article/details/100678524
总结:
果然发现问题所在是最关键的,最怕不晓得错在哪里