springboot中关于继承WebMvcConfigurationSupport后自定义的全局Jackson失效解决方法,localdate返回数组问题
短信预约 -IT技能 免费直播动态提醒
一般情况下我们在config里增加jackson的全局配置文件就能满足基本的序列化需求,比如前后端传参的问题。
@Configurationpublic class JacksonConfig { public static final String LOCAL_TIME_PATTERN = "HH:mm:ss"; public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd"; public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(LOCAL_TIME_PATTERN))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(LOCAL_TIME_PATTERN))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(LOCAL_DATE_PATTERN))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(LOCAL_DATE_PATTERN))); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ofPattern(LOCAL_DATE_TIME_PATTERN))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ofPattern(LOCAL_DATE_TIME_PATTERN))); objectMapper.registerModule(javaTimeModule); objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true); objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configOverride(Boolean.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER)); return objectMapper; }}
但是最近做的项目中发现,请求拦截的时候继承WebMvcConfigurationSupport排除某些例如swagger等目录的情况,上面的配置会失效,所以我们要稍微改动下。看WebMvcConfigurationSupport源码就能发现里面有自己的Jackson处理方法。我们只需要将配置也添加进去就行。
@Configurationpublic class InterceptorConfig extends WebMvcConfigurationSupport { @Bean public HandlerInterceptor getTokenInterceptor() { return new TokenInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getTokenInterceptor()).addPathPatterns(" @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MappingJackson2HttpMessageConverter(objectMapper())); } public static final String LOCAL_TIME_PATTERN = "HH:mm:ss"; public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd"; public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(LOCAL_TIME_PATTERN))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(LOCAL_TIME_PATTERN))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(LOCAL_DATE_PATTERN))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(LOCAL_DATE_PATTERN))); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ofPattern(LOCAL_DATE_TIME_PATTERN))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ofPattern(LOCAL_DATE_TIME_PATTERN))); objectMapper.registerModule(javaTimeModule); objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true); objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configOverride(Boolean.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER)); return objectMapper; }}
也就是把以前直接@Bean注入的形式改成了直接在子类Override然后add进去就行。LocalDateTime返回变成数组也是这个原因。
来源地址:https://blog.csdn.net/weixin_43910915/article/details/132494012
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341