java – 在哪里可以在Spring 3.1中指定Jackson SerializationConfig.Feature设置

我很困惑,为什么使用认的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)

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...