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