为 spring3 mvc 项目定义自定义 jackson objectmapper
我正在使用 mvc:annotation-driven 这意味着只需将 Jackson jar 文件放入配置 Jackson json marhsaller 的类路径中即可。现在我想提供一个自定义对象映射器,以便能够在全局级别上控制日期字段的序列化。
我开始定义 AnnotationMethodhandlerAdapter 并引用我自己的 jsonConverter bean(见下文)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<ref bean="xmlMarshallingConverter" />
</list>
</property>
</bean>
问题是日期仍然被写为时间戳。自定义对象映射器 bean 已创建(已获取日志),但似乎从未使用对象映射器。我假设它仍然使用默认的 MappingJacksonHttpMessageConverter 和默认的 ObjectMapper。
有什么想法吗?我如何确定要覆盖哪些确切的 bean 来更改默认映射器?
谢谢!
I am using mvc:annotation-driven which meant that simply puttin the jackson jar files onto the classpath configured the Jackson json marhsaller. Now I would like to provide a custom object mapper to be able to control the serialization of Date fields on a global level.
I began to define the AnnotationMethodhandlerAdapter and referenced my own jsonConverter bean (see below)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<ref bean="xmlMarshallingConverter" />
</list>
</property>
</bean>
The problem is dates are still written as timestamps. The custom objectmapper bean is created (got logs for that), but it seesm the objectmapper is never used. I assume it still uses the default MappingJacksonHttpMessageConverter and the default ObjectMapper.
Any ideas? How would I figure out what exact beans to overwrite to chagne the default mapper?
Thanx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否将视图解析器配置为也使用自定义对象映射器实例(请参阅下面对 jacksonObjectMapper 的引用)?
据我了解,AnnotationMethodHandlerAdapter 处理入站转换,ContentNegotiatingViewResolver 处理出站转换。
Have you configured the view resolver to also use your custom object mapper instance (see reference to jacksonObjectMapper below)?
It's my understanding that AnnotationMethodHandlerAdapter handles the inbound conversions and ContentNegotiatingViewResolver handles outbound conversions.