Reputation: 1478
I've a project without Spring Boot, but it uses some spring modules like "spring data" and "spring data rest".
I've some problem with the serialization of the java.time.* fields. I've found some tutorials like this but even if I add the following dependency
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
and the following code in my RepositoryRestConfigurerAdapter
@Component public class CvlRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultPageSize(75);
config.setReturnBodyForPutAndPost(Boolean.TRUE);
}
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
super.configureJacksonObjectMapper(objectMapper);
objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
}
}
my actual response regarding a java.time field is like the following
"rateDate" : { "year" : 2017, "month" : "FEBRUARY", "dayOfMonth" : 14, "dayOfWeek" : "TUESDAY", "era" : "CE", "dayOfYear" : 45, "leapYear" : false, "monthValue" : 2, "chronology" : { "id" : "ISO", "calendarType" : "iso8601" }
What I'm doing wrong? What I'm forgetting?
Upvotes: 1
Views: 424
Reputation: 1478
here my adapter. Now it's working
@Component
public class CvlRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultPageSize(75);
config.setReturnBodyForPutAndPost(Boolean.TRUE);
}
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
super.configureJacksonObjectMapper(objectMapper);
objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
SimpleModule sm = new SimpleModule("jsr310module");
sm.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ISO_DATE));
sm.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_DATE));
sm.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME));
sm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
objectMapper.registerModule(sm);
}
}
I only need to check for timezone(ZonedDateTime fields) and encoding(seems to be UTF-8 is the default) and all will be fine. Hope to be useful for someone else
Upvotes: 1