我很困惑,为什么使用默认的jackson包含
Spring似乎已经定制了默认的Jackson配置.
一个设置是混乱的是WRITE_DATES_AS_TIMESTAMPS,Jackson default是真的,但是Spring有某个地方将其更改为false并且还提供了日期格式.
这个世界在哪里呢?我希望我的日期保持序列化为数字.
更新:事实证明,这不是导致问题的春天,它实际上是休眠代理类导致问题.由于某种原因,如果hibernate具有type =“date”的类型映射,则将其作为日期字符串进行序列化,但是如果其类型为“timestamp”,则按预期的顺序排列.而不是花太多时间研究这个问题,我决定现在将所有映射更改为时间戳.
解决方法
从3.1 M1开始,您可以通过注册一个HttpMessageConverters通过mvc:annotation-driven的子元素来指定jackson自定义配置.
见Spring 3.1 MVC Namespace Improvements
请参阅SPR-7504使添加新的消息转换器更容易添加到AnnotationMethodHandlerAdapter
例:
<bean id="jacksonObjectMapper" class="x.y.z.CustomObjectMapper"> </bean> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </mvc:message-converters> </mvc:annotation-driven>
CustomObjectMapper对象
@Component("jacksonObjectMapper") public class CustomObjectMapper extends ObjectMapper { @postconstruct public void afterPropertiesSet() throws Exception { SerializationConfig serialConfig = getSerializationConfig() .withDateFormat(null); //any other configuration this.setSerializationConfig(serialConfig); } }
07002
In addition to constructing instance with specified date format,will enable or disable Feature.WRITE_DATES_AS_TIMESTAMPS (enable if format set as null; disable if non-null)
原文地址:https://www.jb51.cc/java/121942.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。