我在哪里指定 Spring 3.1 中的 Jackson SerializationConfig.Feature 设置 [英] Where do I specify Jackson SerializationConfig.Feature settings in Spring 3.1

查看:44
本文介绍了我在哪里指定 Spring 3.1 中的 Jackson SerializationConfig.Feature 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为什么使用默认包含 jackson 的 Spring 似乎已经自定义了默认的 Jackson 配置.

它弄乱的一个设置是WRITE_DATES_AS_TIMESTAMPSJackson 默认true 但是 Spring 在某处将其更改为 false 并且还提供了日期格式.

这到底发生在什么地方?我希望我的日期保持序列化为数字.

更新:事实证明,导致问题的不是 spring,而是导致问题的休眠代理类.出于某种原因,如果 hibernate 有 type="date" 的类型映射,它会序列化为日期字符串,但如果它的 type="timestamp" 它会按预期序列化.我决定暂时将所有映射更改为时间戳,而不是花太多时间研究这个问题.

解决方案

从 3.1 M1 开始,您可以通过 mvc:annotation 的子元素注册一个 HttpMessageConverters 来指定 jackson 自定义配置-驱动.

参见 Spring 3.1 MVC 命名空间改进

参见 SPR-7504 使向 AnnotationMethodHandlerAdapter 添加新消息转换器更容易

例如:

</bean><mvc:注解驱动><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")公共类 CustomObjectMapper 扩展 ObjectMapper {@PostConstructpublic void afterPropertiesSet() 抛出异常 {SerializationConfig serialConfig = getSerializationConfig().withDateFormat(null);//任何其他配置this.setSerializationConfig(serialConfig);}}

<块引用>

SerializationConfig .withDateFormat

除了构造具有指定日期格式的实例外,将启用或禁用Feature.WRITE_DATES_AS_TIMESTAMPS(如果格式设置为空则启用;如果非空则禁用)

I'm puzzled as to why using a default inclusion of jackson that Spring seems to have customised the default Jackson configuration.

One setting it's messing with is WRITE_DATES_AS_TIMESTAMPS, the Jackson default is true however Spring has somewhere changed this to false and also provided a date format.

Where in the world is this happening? I want my dates to remain serialised as numbers.

UPDATE: Turns out it's not spring that's causing the problem, it's actually hibernates proxy classes causing the problem. For some reason if hibernate has a type-mapping of type="date" it serialises as a date string, though if its type="timestamp" it serialises as expected. Rather than spend too much time looking into this I've decided to just change all my mappings to timestamp for now.

解决方案

Starting with 3.1 M1 you can specify jackson custom configuration by registering an HttpMessageConverters through a sub-element of mvc:annotation-driven.

See Spring 3.1 MVC Namespace Improvements

See SPR-7504 Make it easier to add new Message Converters to AnnotationMethodHandlerAdapter

Exemple:

<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>

The CustomObjectMapper Object

    @Component("jacksonObjectMapper")
    public class CustomObjectMapper extends ObjectMapper {

        @PostConstruct
        public void afterPropertiesSet() throws Exception {

            SerializationConfig serialConfig = getSerializationConfig()     
                        .withDateFormat(null);

                  //any other configuration

            this.setSerializationConfig(serialConfig);
        }
    }

SerializationConfig .withDateFormat

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)

这篇关于我在哪里指定 Spring 3.1 中的 Jackson SerializationConfig.Feature 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆