我在哪里指定Spring 3.1中的Jackson SerializationConfig.Feature设置 [英] Where do I specify Jackson SerializationConfig.Feature settings in Spring 3.1
问题描述
我很困惑为什么使用默认包含的jackson,Spring似乎已经定制了默认的Jackson配置。
I'm puzzled as to why using a default inclusion of jackson that Spring seems to have customised the default Jackson configuration.
其中一个令人烦恼的设置是 WRITE_DATES_AS_TIMESTAMPS
,杰克逊默认为 true
然而Spring在某处将此更改为 false
并且还提供了日期格式。
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 :原来这不是导致问题的春天,它实际上是休眠代理类造成的问题。出于某种原因,如果hibernate的类型映射为 type =date
,它会序列化为日期字符串,但是如果它的 type =timestamp
按预期顺序排列。而不是花太多时间研究这个,我决定暂时将我的所有映射都改为时间戳。
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.
推荐答案
开始使用3.1 M1,您可以通过 mvc:annotation-driven
的子元素注册 HttpMessageConverters
来指定jackson自定义配置。
Starting with 3.1 M1 you can specify jackson custom configuration by registering an HttpMessageConverters
through a sub-element of mvc:annotation-driven
.
参见 SPR-7504 更容易添加新内容消息转换器到AnnotationMethodHandlerAdapter
See SPR-7504 Make it easier to add new Message Converters to 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对象
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);
}
}
< a href =http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/SerializationConfig.html#withDateFormat%28java.text.DateFormat%29 =noreferrer> SerializationConfig。 withDateFormat
SerializationConfig .withDateFormat
除了使用指定的日期格式构建实例外,还将启用或禁用
Feature.WRITE_DATES_AS_TIMESTAMPS(如果格式设置则启用as null;如果非null则禁用)
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屋!