I\'m trying to configure Jackson to show JSR 310 instants in ISO 8601 format.
@Configuration
class Jackson {
@Bean
static ObjectMapper objectMapper(
You can (and probably should) you RepositoryRestConfigurerAdapter
(in Spring Data Rest 2.4) or RepositoryRestMvcConfiguration
which expose the configureObjectMapper
method.
@Configuration
class RepositoryRestAdapterConfiguration extends RepositoryRestConfigurerAdapter {
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
}
}
Actually, you could make it more bootify just using jackson auto-configuration properties in application.properties (or application.yml):
spring.jackson.serialization.write_dates_as_timestamps=false
After some experimentation, I found that if I annotate my "setter" (setter injection) with it will get run. This is a little simpler, and cleaner than using field injection and @PostConstruct
.
@Configuration
class Jackson {
@Autowired
void configureObjectMapper( final ObjectMapper objectMapper ) {
objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
}
}
I think one way could be to inject the ObjectMapper
in a suitable bean class and then do the setting in a @PostConstruct
method, e.g.:
@Configuration // or @Service or some bean
public class SomeClass ... {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
private void configureObjectMapper() {
objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
}
}