Spring Interceptor 不生效


此处用到的是spring-boot 2.2.5.RELEASE版本,对应的spring-mvc 5.2.4.RELEASE

发现WebMvcConfigurerAdapter不推荐使用了,推荐WebMvcConfigurer然后就这样写了

@Configuration
public class WebConfig implements WebMvcConfigurer {

    public WebConfig() {
        System.out.println("WebConfig init");
    }

    @Autowired
    private JSONModelInterceptor jsonModelInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jsonModelInterceptor).addPathPatterns("/**").excludePathPatterns("/*/assets/**");// 排除不需要拦截的资源
    }
}

结果新写的拦截器JSONModelInterceptor始终不生效,换回继承WebMvcConfigurerAdapter也是一样,
WebConfig init也都输出了,你还要我怎样...

后来各种搜索引擎,改为继承WebMvcConfigurationSupport 拦截器生效了

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
...

你以为就结束了?


然而另外一个ResourceConfig失效了,这。。

@Configuration
public class ResourceConfig extends WebMvcConfigurationSupport {
...

又各种各种搜索引擎,发现WebMvcConfigurationSupport这货一个项目里面 只能有一个 只能有一个 只能有一个 这是个大坑

之后又发现后台的日期变成时间戳了,明明有配置

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

又各种搜索引擎,找到https://blog.csdn.net/qq_30912043/article/details/80967352

现在去掉WebMvcConfigurationSupport改为实现WebMvcConfigurer ,这些问题都解决了,感谢网友感谢搜索引擎。

总结:
配置拦截器、静态资源等不要继承WebMvcConfigurationSupport,改实现WebMvcConfigurer,因为WebMvcConfigurationSupport会覆盖一些默认配置信息。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM