Spring Jackson custom Deserializer does not called

491

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()));
   }
}
Share:
491

Related videos on Youtube

user3057944
Author by

user3057944

Updated on January 01, 2023

Comments

  • user3057944
    user3057944 over 1 year

    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?

  • jmiguel
    jmiguel over 10 years
    That's the problem, it's NOT running, or at least there's no process, no indication at all of the program running and how could there be if I deleted the installation folder in which the path to executable was: C:\Program Files (x86)\Faronics\Deep Freeze\Install\C-0\DFServ.exe. But somehow it waited for the 5 or 6 thawed reboots I mentioned earlier and now it's back on business freezing C: again. Main problem: I don't have installation folder so I can't uninstall it, is there any other approach to fix this that doesn't need uninstalling the program to make it stop working???
  • jmiguel
    jmiguel over 10 years
    No, it won´t. When I run it, it displays the error message I wrote earlier: "Deep Freeze Standard must be disabled before any Install/Uninstall can proceed"
  • Kirk
    Kirk over 10 years
    I have no idea now but, Any SafeMode tries? did you try that?
  • Kirk
    Kirk over 10 years
    check my updated post
  • Kirk
    Kirk over 10 years
    yes, maybe another PC, however you need that installation foler and files
  • jmiguel
    jmiguel over 10 years
    Oh, about that file Persi0.sys, yeah, I might have deleted it too, looks like I deleted all my options away,ggg.
  • jmiguel
    jmiguel over 10 years
    that's right, won't I get those if I install it in another pc, including the Persi0.sys file??
  • Kirk
    Kirk over 10 years
    you just copy all files from new installation folder and try
  • user3057944
    user3057944 over 2 years
    I define ObjectMapper bean in '@Configuration' class. I don't want to use ObjectMapper "manually", I was told Jackson use that ObjectMapper defined at Bean. But it seems to it it does not.
  • user3057944
    user3057944 over 2 years
    You could right, maybe Jackson does not use that ObjectMapper I define in my configuration class. So I added the Bean you advised, but when Jackson deserializing a Date, my deserializer still is not called.
  • João Dias
    João Dias over 2 years
    Well, then the only option is that you are importing a different Date in your ObjectMapper than the one that you have in your model. It is highly unlikely that this is the case, but can you double-check it? Minor detail that is not related: you have mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBT‌​YPE, false); duplicated in your configuration.
  • ray
    ray over 2 years
    Can you add code for your DateDeserializer and add where you deserialize.
  • ray
    ray over 2 years
    Also make sure your configuration class in same base-package
  • João Dias
    João Dias over 2 years
    Great! You can upvote answers if they helped you ;)