最近在項(xiàng)目中用類繼承WebMvcConfigurationSupport實(shí)現(xiàn)攔截器
@Configuration
@RequiredArgsConstructor
public class SpringWebSupport extends WebMvcConfigurationSupport {
private final ProjectInterceptor projectInterceptor;
// 攔截器
//設(shè)置攔截器對(duì)象和攔截請(qǐng)求
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterceptor).addPathPatterns("
/**").excludePathPatterns("......"); //配置攔截路徑
}
}
同時(shí)呢,在我曾經(jīng)寫過的一個(gè)依賴包里(其他包),配置了一個(gè)消息轉(zhuǎn)換器并且在項(xiàng)目中引入
public class JacksonObjectMapper extends ObjectMapper{
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper () {
super();
//收到未知屬性時(shí)不報(bào)異常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化時(shí),屬性不存在的兼容處理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance) // 對(duì)于long類型轉(zhuǎn)為String
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注冊(cè)功能模塊 例如,可以添加自定義序列化器和反序列化器
this.registerModule(simpleModule);
}
}
@Slf4j
@Configuration
public class MyWebConfig extends WebMvcConfigurationSupport {
/**
* 擴(kuò)展 mvc 框架的消息轉(zhuǎn)換器
* @param converters
*/
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// 創(chuàng)建消息轉(zhuǎn)換器對(duì)象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
// 設(shè)置該消息轉(zhuǎn)換器使用 JacksonObjectMapper 進(jìn)行轉(zhuǎn)換
messageConverter.setObjectMapper(new JacksonObjectMapper());
// 將消息轉(zhuǎn)換器對(duì)象追加到 mvc 框架的轉(zhuǎn)換器集合中(加到最前面)
converters.add(0, messageConverter);
}
}
在啟動(dòng)類掃描了這另一個(gè)包里的WebMvcConfigurationSupport配置依賴。
希望的是:攔截器+消息轉(zhuǎn)變換器全都生效,但是生效的只有攔截器,Long類型還是沒有轉(zhuǎn)換成String返回給前端。
DEBUG斷點(diǎn)調(diào)試
發(fā)現(xiàn)啟動(dòng)的時(shí)候根本不會(huì)走這里,說明沒生效
解決辦法:把添加消息轉(zhuǎn)換器代碼遷移到和攔截器項(xiàng)目下同一個(gè)WebMvcConfigurationSupport里文章來源:http://www.zghlxwxcb.cn/news/detail-643098.html
直接就成了文章來源地址http://www.zghlxwxcb.cn/news/detail-643098.html
到了這里,關(guān)于多個(gè)配置WebMvcConfigurationSupport失效問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!