Spring Boot Jackson2ObjectMapperBuilderCustomizer无法正常工作

发布于 2025-02-13 16:49:45 字数 1939 浏览 0 评论 0原文

我想使用以下配置来自定义Java8 LocalDateTime模式

@Bean
@Primary
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return builder -> {
        builder.simpleDateFormat(dateTimeFormat)
                .serializerByType(LocalDateTime.class,
                        new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)))
                .deserializerByType(LocalDateTime.class,
                        new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)))

                .serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)))
                .deserializerByType(LocalDate.class,
                        new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)))

                .serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)))
                .deserializerByType(LocalTime.class,
                        new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
    };
}

但不起作用,Spring Boot版本2.6.9,该项目集成了春季云组件,例如Feing Eureka等。但是在我的Spring Boot Project中,没有Spring Cloud Cloud Coponents,上面的config工作。 ,

“在此处输入图像说明”

最后我配置了httpmessageconverters解决此问题?

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    // jackson单独配置,参考:https://segmentfault.com/a/1190000041997183
    converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
    converters.add(0, mappingJackson2HttpMessageConverter);
}

这两个项目都启用Weablewbmvc并扩展了WebMvcconFigurer以配置拦截器

    @Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

I want to use the following config to customize java8 localdatetime pattern

@Bean
@Primary
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return builder -> {
        builder.simpleDateFormat(dateTimeFormat)
                .serializerByType(LocalDateTime.class,
                        new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)))
                .deserializerByType(LocalDateTime.class,
                        new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)))

                .serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)))
                .deserializerByType(LocalDate.class,
                        new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)))

                .serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)))
                .deserializerByType(LocalTime.class,
                        new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
    };
}

but not working,spring boot version 2.6.9,the project integrate spring cloud components like feing eureka and so on.but in my spring boot project without spring cloud coponents,above config working,

enter image description here

finally i config the httpmessageconverters to resolve this issue,but i did not know why above config not working?

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    // jackson单独配置,参考:https://segmentfault.com/a/1190000041997183
    converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
    converters.add(0, mappingJackson2HttpMessageConverter);
}

Both projects EnableWebMVC and extends WebMvcConfigurer to config interceptors

    @Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文