user3057944
user3057944

Reputation: 711

Spring Jackson custom Deserializer does not called

I have a Spring project, and I try to add a custom deserializer to deserialize Date properties depend on their format. If I use it as annotation on Date property, it works fine. But if I add the deserializer to my object mapper, it does not called when Jackson deserialize a date.

I try to apply my custom deserializer like this:

    @Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Date.class, new DateDeserializer());
    mapper.registerModule(module);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
    return mapper;
}

I don't want to apply an annotation on Date properties every time, I want to use this deserializer by default. What I do wrong?

Upvotes: 1

Views: 2211

Answers (3)

user3057944
user3057944

Reputation: 711

Thanks the help for everyone. Finally I found the answer at spring.io.

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.deserializerByType(Date.class, new DateDeserializer());
    converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
   }
}

Upvotes: 1

ray
ray

Reputation: 1670

I have doubt how you try to use this ObjectMapper bean inside your application.

I trust you already know that this bean is need to created inside a Configuration class. If not your bean will not register in the context. Like this for example,

@Configuration
public class MapperConfig {
    
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Date.class, new DateDeserializer());
        mapper.registerModule(module);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
        mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
        return mapper;
    }
}

The second problem might be how you try use this ObjectMapper bean. If your create new instance of ObjectMapper like this ObjectMapper objectMapper = new ObjectMapper();, that instance will not have your custom deserializer and stuff. What you can do is @Autowire the ObjectMapper instance that you have already created.

Upvotes: 0

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17460

Given that if you use the deserializer in an annotation on Date property then I would say that this ObjectMapper is not being used for deserialization. Try the following:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
   MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
   jsonConverter.setObjectMapper(objectMapper);
   return jsonConverter;
}

Upvotes: 0

Related Questions