本文介绍了Spring,Jackson和Customization(例如CustomDeserializer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Spring仍然有些不熟悉,我遇到了一个问题:这使得有必要为杰克逊实施我的自定义反序列化器。该过程在一个小的中描述,但是,我被Spring困住了。我不明白,在哪里

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();

当json通过控制器类的方法反序列化时执行。所以我不知道如何通过自定义反序列化器替换默认的反序列化器。

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.

推荐答案

你没有说你在春天如何使用杰克逊,所以我假设你通过< mvc使用它:注释驱动/> @RequestBody 和/或 @ResponseBody 注释。

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 @RequestBody and/or @ResponseBody annotations.

< mvc:annotation-driven /> 所做的一件事就是注册 AnnotationMethodHandlerAdapter bean附带了许多预先配置的 HttpMessageConverter bean,包括 MappingJacksonHttpMessageConverter ,用于处理与Jackson注释的模型类之间的编组。

One of the things that <mvc:annotation-driven/> 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.

现在 MappingJacksonHttpMessageConverter 有一个 setObjectMapper()方法,它允许您覆盖默认的 ObjectMapper 。但由于 MappingJacksonHttpMessageConverter 是由< mvc:annotation-driven /> 在幕后创建的,你不能到了它。

Now MappingJacksonHttpMessageConverter has a setObjectMapper() method, which allows you to override the default ObjectMapper. But since MappingJacksonHttpMessageConverter is created behind the scenes by <mvc:annotation-driven/>, you can't get to it.

然而,< mvc:annotation-driven /> 只是一个方便的短 - 切。声明你自己的 AnnotationMethodHandlerAdapter bean是有效的,向你注入你自己的 MappingJacksonHttpMessageConverter bean(通过 messageConverters property),并将自己的自定义 ObjectMapper 注入其中。

However, <mvc:annotation-driven/> 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.

然后你遇到了如何构建自定义 ObjectMapper 的问题,因为它不是一个非常适合Spring的类。我建议写你的的简单实现。

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:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
   <property name="messageConverters">
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
         <property name="objectMapper">
            <bean class="com.x.MyObjectMapperFactoryBean"/>
         </property>
      </bean>
   </property>
</bean>

这篇关于Spring,Jackson和Customization(例如CustomDeserializer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 03:42