Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot ignore ObjectMapper module

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?

like image 558
rascio Avatar asked Apr 11 '15 20:04

rascio


People also ask

How do I ignore properties in ObjectMapper?

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.

Can we Autowire ObjectMapper spring boot?

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.

Is ObjectMapper thread safe?

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.

How do I enable Jackson objectmapper in Spring Boot?

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.

How to configure the objectmapper from different configurations or modules?

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.

Why should I register a Jackson module in Spring Boot?

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.

Does jackson2objectmapperbuilder register all modules on the classpath?

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.


1 Answers

To make it work the @Configuration class should extend WebMvcAutoConfiguration

like image 68
rascio Avatar answered Oct 20 '22 16:10

rascio