Reputation: 2355
To serialize deserialize object I am useing Jackson as flow
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate openingDate
How do I make this the default globally so I do not have to add it to every property ?
Using XML configuration.
Upvotes: 4
Views: 27389
Reputation: 325
I tried some of answered solutions but none of them worked for me and this is what i did
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.forEach(converter -> {
if(converter.getClass().equals(MappingJackson2HttpMessageConverter.class)) {
ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
}
});
}
And use extendMessageConverters
instead of configureMessageConverters
since it turns off default converter registration.
Upvotes: 0
Reputation: 481
One more solution similar to @Jaiwo99 in the thread. We can register JavaTime module directly to ObjectMapper
@Bean
public Object registerJavaTimeModuleForJackson(){
return new Object(){
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void registerJavaTimeModuleForJackson(){
objectMapper.registerModule(new JavaTimeModule());
}
};
}
I am using springboot 2.4.5
Upvotes: 1
Reputation: 93
With Spring Boot you can achieve this by registering new Module
.
@Configuration
public class AppConfig {
@Bean
public Module module() {
SimpleModule module = new SimpleModule("Module", new Version(1, 0, 0, null, null, null));
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
return module;
}
}
As stated in documentation here
Jackson 1.7 added ability to register serializers and deserializes via Module interface. This is the recommended way to add custom serializers -- all serializers are considered "generic", in that they are used for subtypes unless more specific binding is found.
and here:
Any beans of type
com.fasterxml.jackson.databind.Module
are automatically registered with the auto-configuredJackson2ObjectMapperBuilder
and are applied to anyObjectMapper
instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.
Upvotes: 6
Reputation: 4517
You can use configure your serializers (providing fully qualified class name) in spring Jackson2ObjectMapperFactoryBean
then bind it with MappingJackson2HttpMessageConverter
. Here is an example XML
configuration snippet:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.web.context.support.Jackson2ObjectMapperFactoryBean"
p:failOnEmptyBeans="false"
p:indentOutput="true">
<property name="serializers">
<array>
<bean class="LocalDateSerializer" />
</array>
</property>
</bean>
</property>
</bean>
The link to the documentation
Upvotes: 2
Reputation: 4137
If you are using Java-based configuration, you can create your configuration class extending WebMvcConfigurerAdapter and do the following:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
converter.setObjectMapper(objectMapper);
converters.add(converter);
super.configureMessageConverters(converters);
}
In here, you can configure the ObjectMapper as you like and set it as a converter.
Upvotes: 10
Reputation: 10017
Well you can install modules for using things like java datetime or jodatime. checkout this:
@Bean
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
return new Jackson2ObjectMapperBuilder()
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.findModulesViaServiceLoader(true)
.modulesToInstall(module);
}
Upvotes: 3