问题描述
对Spring还是有点陌生,遇到了问题 这使得有必要为 Jackson 实现我的自定义解串器.该过程在一个小的 教程 中有所描述,但是,我坚持使用 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();
在Spring MVC中,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.
欢迎提出任何建议.
推荐答案
你没有说你在 Spring 中是如何使用 Jackson 的,所以我假设你是通过 <mvc:annotation 使用它的-driven/>
和 @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.
然而, 只是一个方便的捷径.声明您自己的
AnnotationMethodHandlerAdapter
bean,将您自己的 MappingJacksonHttpMessageConverter
bean(通过 messageConverters
属性)注入其中,并注入您的自己定制的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 的类.我建议写你的 FactoryBean
的简单实现.
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 和自定义(例如 CustomDeserializer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!