3

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

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204

1 Answers1

5

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;
}
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Do you know why this doesn't have any effect and how could.I debug this ? As I said, the deserialiser serialize never get fired – Dimitri Kopriwa Dec 11 '16 at 19:50
  • How can I add them directly to the object mapper ? I don't get it sorry – Dimitri Kopriwa Dec 11 '16 at 19:58
  • You can try that without registering java time module. Add ser/deser using simple module and register the module in the object mapper.SimpleModule module = new SimpleModule(); module.addSerializer(LocalDate.class, new LocalDateSerializer()); mapper.registerModule(module); something like this. – s7vr Dec 11 '16 at 19:58
  • Using SimpleModule instead of JavaTimeModule didn't work, It didn't change anything, serialization is ignored. – Dimitri Kopriwa Dec 11 '16 at 20:18
  • Not sure why I was down voted but added imports for jsr310 ser/deser for clarity. – s7vr Dec 11 '16 at 23:54
  • I didn't downvote, I just up vote since this must be correct. It's still doesn't work for me :/ – Dimitri Kopriwa Dec 12 '16 at 02:01