此處用到的是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
會覆蓋一些默認配置信息。