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 described in a small tutorial, however, I am stuck with Spring. I do not understand, where
ObjectMapper mapper = new ObjectMapper();
in Spring MVC is carried out when json is deserializes by a method of a controller class. So I do not know, what to do in order to replace the default deserializer by a custom deserialiser.
Any suggestions most welcome.
You don’t say how you’re using Jackson in Spring, so I’ll assume you’re using it through
<mvc:annotation-driven/>and the@RequestBodyand/or@ResponseBodyannotations.One of the things that
<mvc:annotation-driven/>does is to register aAnnotationMethodHandlerAdapterbean which comes with a number of pre-configuredHttpMessageConverterbeans, includingMappingJacksonHttpMessageConverter, which handles marshalling to and from Jackson-annotated model classes.Now
MappingJacksonHttpMessageConverterhas asetObjectMapper()method, which allows you to override the defaultObjectMapper. But sinceMappingJacksonHttpMessageConverteris created behind the scenes by<mvc:annotation-driven/>, you can’t get to it.However,
<mvc:annotation-driven/>is just a convenient short-cut. It’s just as a valid to declare your ownAnnotationMethodHandlerAdapterbean, injecting into it your ownMappingJacksonHttpMessageConverterbean (via themessageConvertersproperty), and injecting your own customizedObjectMapperinto 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 ofFactoryBean.So you’d end up with something like this: