Spring, Jackson and Customization (e.g. CustomDeserializer)

前端 未结 6 1658
情深已故
情深已故 2020-11-30 02:42

Being still a little unfamiliar with Spring, I have encountered a problem that makes it necessary implementing my a custom deserialzer for Jackson. The procedure is describe

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 03:16

    You don't say how you're using Jackson in Spring, so I'll assume you're using it through and the @RequestBody and/or @ResponseBody annotations.

    One of the things that does is to register a AnnotationMethodHandlerAdapter bean which comes with a number of pre-configured HttpMessageConverter beans, including MappingJacksonHttpMessageConverter, which handles marshalling to and from Jackson-annotated model classes.

    Now MappingJacksonHttpMessageConverter has a setObjectMapper() method, which allows you to override the default ObjectMapper. But since MappingJacksonHttpMessageConverter is created behind the scenes by , you can't get to it.

    However, is just a convenient short-cut. It's just as a valid to declare your own AnnotationMethodHandlerAdapter bean, injecting into it your own MappingJacksonHttpMessageConverter bean (via the messageConverters property), and injecting your own customized ObjectMapper into that.

    You then have the problem of how to build a custom ObjectMapper, since it's not a very Spring-friendly class. I suggest writing your own simple implementation of FactoryBean.

    So you'd end up with something like this:

    
       
          
             
                
             
          
       
    
    

提交回复
热议问题