在Spring 3.1中我应在哪里指定Jackson SerializationConfig.Feature设置
问题内容:
对于为什么使用默认的杰克逊(Jackson),Spring似乎已自定义默认的杰克逊配置,我感到困惑。
它所困扰的一个设置是WRITE_DATES_AS_TIMESTAMPS
,Jackson的默认设置是,true
但是Spring在某处将其更改为,false
并且还提供了日期格式。
这到底在哪里发生?我希望我的日期保持序列化为数字。
更新
:事实证明,不是弹簧导致问题的原因,实际上是hibernate代理类导致的问题。由于某种原因,如果hibernate具有类型映射,type="date"
则它将序列化为日期字符串,尽管如果type="timestamp"
它按预期序列化。我决定不花太多时间研究这个问题,而是决定暂时将所有映射更改为时间戳。
问题答案:
从3.1 M1开始,您可以通过注册的HttpMessageConverters
子元素来指定杰克逊自定义配置mvc:annotation- driven
。
请参阅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);
}
}
SerializationConfig
.withDateFormatIn 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)