In application context I have registered an ObjectMapper module:
@Bean
public SimpleModule jsr310Module() {
final SimpleModule module = new SimpleModule();
module.addSerializer(new LocalDateSerializer());
module.addDeserializer(LocalDate.class, new LocalDateDeserializer());
return module;
}
I tried to debug and it is loaded (or at least the method public void setupModule(SetupContext context)
is execute on boot)
but when I call a rest API that return an object with a LocalDate my deserializer is ignored.
Some hint to solve the problem?
ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.
If you're working with Spring Boot, there's a section in the manual dedicated to working with the ObjectMapper If you create a default Jackson2ObjectMapperBuilder @Bean , you should be able to autowire that same ObjectMapper instance in your controller.
ObjectMapper is a completely thread-safe service class, it is meant to be used as singleton across the lifetime of the application. It is also very expensive to create.
With Spring Boot. As described in the Spring Boot reference documentation, there are various ways to customize the Jackson ObjectMapper. You can for example enable/disable Jackson features easily by adding properties like spring.jackson.serialization.indent_output=true to application.properties.
The configuration beans are applied in a specific order, which we can control using the @Order annotations. This elegant approach is suitable if we want to configure the ObjectMapper from different configurations or modules. Another clean approach is to define a Jackson2ObjectMapperBuilder bean.
This is useful if you want to use advanced Jackson configuration not exposed through regular configuration keys. If you just need to register an additional Jackson module, be aware that Spring Boot autodetects all Module @Bean.
According to the Jackson2ObjectMapperBuilder documentation, it will also register some modules if they're present on the classpath: The advantage of this approach is that the Jackson2ObjectMapperBuilder offers a simple and intuitive way to build an ObjectMapper.
To make it work the @Configuration
class should extend WebMvcAutoConfiguration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With