【问题标题】:LocalDateTime is representing in array formatLocalDateTime 以数组格式表示
【发布时间】:2020-12-20 06:29:25
【问题描述】:

我在 Java 8 中使用 spring boot 2.2.6 和 Jackson 2.10.3。我在整个项目中使用 localdatetime 对象。 Jackson 无法正确解析 LocalDateTime(或者它可能是默认格式)并在 json 响应中发送日期,如下面的数组格式

        "createdDate": [
            2020,
            8,
            31,
            0,
            0,
            0,
            80000000
        ]

JSON Java 8 LocalDateTime format in Spring Boot 中所述,Spring boot 2 已经在类路径上具有默认的 jackson-datatype-jsr310:2.10.3。我希望整个项目中的日期在 json 中表示为 2020-03-31:00。第一个解决方案在上面的链接中不起作用。之后,我尝试了 @JsonSerialize 注释并且它有效,但我不想应用于每个类。所以也试图覆盖对象映射器,但它没有工作

@Primary
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        SimpleModule module = new SimpleModule("my custom date serializer");
        module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer());
        mapper.registerModule(module);
        return mapper;
    }

也试过自定义Jackson2ObjectMapperBuilder,但还是有数组格式的日期

@Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        SimpleModule module = new SimpleModule("my custom date serializer");
        module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer());
        builder.modulesToInstall(modules)
        return builder;
    }

也尝试过 Jackson2ObjectMapperBuilderCustomizer

@Configuration
public class JacksonConfiguration {

    @Primary
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        };
    }
}

控制器

@RestConroller
@RequestMapping("/user")
UserController {

    User getUser(){
        User user = new User()
        user.createdDate = LocalDateTime.now();
        return user;
    }

}

我可以在全球范围内做些什么,所以项目中的每个日期都将被序列化为字符串格式,如 2020-09-01 ?

任何建议都会有所帮助。

【问题讨论】:

    标签: java spring-boot objectmapper jackson2


    【解决方案1】:

    罪魁祸首是@EnableWebMVC。我的一个 cors 课程有 @EnableWebMVC 注释。这是禁用 spring boot 自动配置并覆盖映射器配置。这不会让您覆盖对象映射器的配置。通过删除 @EnableMVC 注释,现在可以正常工作了。在SpringBoot2中,我们不需要做任何与日期序列化显式相关的配置,它会自动解析“yyyy-mm-dd”格式的java 8日期。

    【讨论】:

    • 在迷路寻找答案后,感谢您分享这个。像魅力一样工作!
    【解决方案2】:

    创建一个Jackson2ObjectMapperBuilderCustomizer bean:

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            builder.simpleDateFormat(dateTimeFormat);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
        };
    }
    

    将属性spring.jackson.serialization.write_dates_as_timestamps=false 添加到application.properties,或者您也可以在Jackson2ObjectMapperBuilderCustomizer 中以编程方式完成。

    【讨论】:

    • @PrabjotSingh 听起来您正在从 Spring 无法控制的地方进行序列化。发布您的序列化代码。
    • 我已经编辑了这个问题。添加 Jackson2ObjectMapperBuilderCustomizer 代码和控制器代码。
    • 还是有同样的问题,尝试使用prop文件和java配置。
    • @PrabjotSingh 抱歉,我不知道还有什么可以帮助您的。自己调试代码。
    • 感谢先生的帮助,我得到了答案。我贴出来了。
    猜你喜欢
    • 2014-04-26
    • 2017-01-01
    • 1970-01-01
    • 2014-11-02
    • 2016-12-13
    • 2013-08-26
    • 2016-07-27
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多