如何为java.time.LocalDateTime

时间:2018-08-24 01:54:03

标签: java json jackson

Jackson的JavaTimeModule 序列化/反序列化 java.time遍及全球,但其默认日期时间格式为ISO标准,例如2018-01-10T10:20:30代表LocalDateTime,2018-01-10T10:20:30+08:00代表OffsetDateTime 。但是我需要为LocalDateTime和OffsetDateTime设置一个像2018-01-10 10:20:30这样的全局本地格式,而没有T和OffsetTime(使用本地默认的OffsetTime)。我该怎么办?

  

注意:我了解@JsonFormat@JsonSerialize@JsonDeserialize。那不是全局设置。

3 个答案:

答案 0 :(得分:0)

您可以配置一个ObjectMapper。本页说明https://www.baeldung.com/jackson-serialize-dates的方式。我认为您希望该页面上的示例4接近。

然后,您需要使该对象成为全局对象。不同的框架使用不同的方法。本页说明如何在Spring和Spring-boot Configuring ObjectMapper in Spring中执行此操作。对于其他人,仅Google即可。

答案 1 :(得分:0)

弹簧靴

    @SpringBootApplication
    public class Application implements Jackson2ObjectMapperBuilderCustomizer {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }

        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(formatter);

            builder.failOnEmptyBeans(false) // prevent InvalidDefinitionException Error
                    .serializerByType(LocalDateTime.class, localDateTimeSerializer);
        }
    }

Springboot和Spring框架

    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(formatter);
            LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(formatter);

            JavaTimeModule module = new JavaTimeModule();
            module.addSerializer(LocalDateTime.class, localDateTimeSerializer);
            module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);

            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(module);
            // add converter at the very front
            // if there are same type mappers in converters, setting in first mapper is used.
            converters.add(0, new MappingJackson2HttpMessageConverter(mapper));
        }
    }

希望这可以为您提供帮助。

答案 2 :(得分:0)

如果您在全局范围内使用 ObjectMapper 的单个实例(并且想要一个独立于 Spring/Java 8 jackson 模块的解决方案),您可以执行以下操作:

public ObjectMapper getCustomConfigMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    final SimpleModule module = new SimpleModule();
    module.addDeserializer(LocalDateTime.class, new CustomLocaDateTimeDeserializer());
    mapper.registerModule(module);
    return mapper;
}

public static class CustomLocaDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    public static final DateTimeFormatter CUSTOM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext __) throws IOException {
        final String value = jsonParser.getText().strip();
        return LocalDateTime.parse(value, CUSTOM_FORMATTER);
    }
}