在春季使用JSR310 java.time时将日期序列化为ISO 8601 [英] Serialize Date, Instant to ISO 8601 when using JSR310 java.time in spring

查看:188
本文介绍了在春季使用JSR310 java.time时将日期序列化为ISO 8601的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JSR310替换JodaTime,并且JodaTime()的模块运行正常.

I am replacing JodaTime by JSR310 and the module of JodaTime() was working fine.

我正在尝试在spring-boot应用程序中重新配置日期的序列化.

I am trying to reconfigure the serialization of my dates in my spring-boot application.

我不能同时保留两者,因此我正在寻找一种将日期序列化/反序列化为ISO 8601的方法.

I can't keep both so I am looking for a way to serialize/deserialize my date to ISO 8601.

我遵循了这里的建议,但这无济于事: http://lewandowski.io/2016/02/使用json格式化Java时间并使用json/

I followed the advices here but this doesn't help : http://lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/

这是我的JacksonConfig.javaobjectMapper:

@Configuration
public class JacksonConfig extends RepositoryRestMvcConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(JacksonConfig.class);
    public static final DateTimeFormatter FORMATTER = ofPattern("dd::MM::yyyy");

    @Bean
    // Override and Primary due to bug: https://github.com/spring-projects/spring-boot/issues/6529
    @Override
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
        mapper.registerModule(javaTimeModule);
        return mapper;
    }

    public class LocalDateSerializer extends JsonSerializer<LocalDate> {
        @Override
        public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeString(value.format(FORMATTER));
        }
    }
    public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
        @Override
        public LocalDate deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
            return LocalDate.parse(p.getValueAsString(), FORMATTER);
        }
    }
}

实例化了bean,但是serializedeserialize从未被调用.

The bean get instanciated but the serialize and deserialize never get called.

我还尝试了没有自定义序列化器/反序列化器的JavaTimeModule,它也无法正常工作.

I've also tried the JavaTimeModule without the custom serializer/deserializer, it's also not working.

似乎根本没有效果.

是否有一种方法可以对此进行更多调试?

Is there a way to debug this a bit more ?

我正在使用spring-hateoas和spring-data,我遇到了可能与以下问题有关的问题:

I am using spring-hateoas and spring-data, I have this issue that might be related :

https://github.com/spring-projects/spring-hateoas/issues/333

推荐答案

使用Java时间模块中提供的ser/deser.实际上,这是使用自定义格式程序设置的.

Make use of the ser/deser provided in the java time module. This actually sets them with the custom formatter.

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;

public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    LocalDateSerializer localDateSerializer = new LocalDateSerializer(FORMATTER);
    javaTimeModule.addSerializer(LocalDate.class, localDateSerializer);
    LocalDateDeserializer localDateDeserializer = new LocalDateDeserializer(FORMATTER);
    javaTimeModule.addDeserializer(LocalDate.class, localDateDeserializer);
    mapper.registerModule(javaTimeModule);
    return mapper;
}

这篇关于在春季使用JSR310 java.time时将日期序列化为ISO 8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆