文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

springboot中关于继承WebMvcConfigurationSupport后自定义的全局Jackson失效解决方法,localdate返回数组问题

2023-08-30 18:00

关注

一般情况下我们在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

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-人工智能
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯