Serialize Date, Instant to ISO 8601 when using JSR310 java.time in spring

点点圈 提交于 2019-12-07 13:15:45

问题


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

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

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

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

This is my JacksonConfig.java with the objectMapper:

@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);
        }
    }
}

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

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

It seems to have no effect at all.

Is there a way to debug this a bit more ?

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


回答1:


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;
}


来源:https://stackoverflow.com/questions/41089005/serialize-date-instant-to-iso-8601-when-using-jsr310-java-time-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!