json Jackson 序列化配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5110586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson serializationConfig
提问by Sven Haiges
I am using Hymanson JSON in a Spring 3 MVC app. To not serialize each and every single Date field, I created a custom objectmapper that uses a specific DateFormat:
我在 Spring 3 MVC 应用程序中使用 Hymanson JSON。为了不序列化每个日期字段,我创建了一个使用特定 DateFormat 的自定义对象映射器:
@Component("HymansonObjectMapper")
public class CustomObjectMapper extends ObjectMapper
{
Logger log = Logger.getLogger(CustomObjectMapper.class);
@PostConstruct
public void afterProps()
{
log.info("PostConstruct... RUNNING");
//ISO 8601
getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SZ"));
}
//constructors...
}
This custom ObjectMapper is injected into the JsonConverter:
这个自定义的 ObjectMapper 被注入到 JsonConverter 中:
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="objectMapper" ref="HymansonObjectMapper" /> <!-- defined in CustomObjectMapper -->
</bean>
There is no exception in the logs and serialization works, but it is not picking up the dateformat, it simple serializes to a timestamp. The @PostConstruct annotation works, the log statement in the method is in the logs.
日志和序列化工作也不例外,但它没有选择日期格式,它简单地序列化为时间戳。@PostConstruct 注释有效,方法中的日志语句在日志中。
Does anyone know why this fails?
有谁知道为什么会失败?
回答by StaxMan
You may also need to specify that you want textual Date serialization, by doing:
您可能还需要通过执行以下操作来指定您想要文本日期序列化:
configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
(although I was assuming setting non-null date format might also trigger it, but maybe not)
(虽然我假设设置非空日期格式也可能触发它,但也许不会)
Also, you can do configuration of mapper directly from constructor (which is safe). Not that it should change behavior, but would remove need for separate configuration method.
此外,您可以直接从构造函数(这是安全的)配置映射器。并不是说它应该改变行为,而是不需要单独的配置方法。
回答by Adam Evans
I've done the below which works to get around compatability with Java / PHP timestamps. Java uses milliseconds since EPOCH and PHP uses seconds so was simpler to use ISO dates.
我已经完成了以下工作,以解决与 Java/PHP 时间戳的兼容性问题。Java 使用毫秒,因为 EPOCH 和 PHP 使用秒,所以使用 ISO 日期更简单。
I declare the below message adapters:
我声明以下消息适配器:
<bean id="messageAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="HymansonJsonMessageConvertor"
class="my.app.MyMappingHymansonHttpMessageConverter"/>
</list>
</property>
</bean>
And MyMappingHymansonHttpMessageConverter looks like the below:
MyMappingHymansonHttpMessageConverter 如下所示:
public class MyMappingHymansonHttpMessageConverter extends MappingHymansonHttpMessageConverter {
public MyMappingHymansonHttpMessageConverter(){
super();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
setObjectMapper(objectMapper);
}
}
With the above all dates are written out in ISO format.
以上所有日期都以 ISO 格式写出。

