处理 DateTime 的 CustomJacksonMapper 配置在 Spring 4.1.5 上不起作用
Configuration of CustomJacksonMapper to deal with DateTime is not working on Spring 4.1.5
以下是我所做的配置:
我的配置文件:
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Support for Joda Time -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="customJacksonMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
我的 class 扩展了 Object
@Component("customJacksonMapper")
public class CustomJacksonMapper extends ObjectMapper {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* Instantiates a new custom jackson mapper.
*
* RegisterModule = Registar o módulo do JodaTime.
* Locale = Padrão portugues Brasil.
* TimeZone = Converte para o timezone de São Paulo.
*
*/
public CustomJacksonMapper() {
this.registerModule(new JodaModule());
this.setLocale(new Locale("pt_BR"));
this.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS , false);
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
使用此配置,正如我在很多帖子中所读到的,日期应该以 Json 中的正确格式返回,但我得到的是以下内容:
{"date": 1467920285301}
我做错了什么?
尝试将这一行添加到构造函数中:
this.setDateFormat(new ISO8601DateFormat());
我将 Spring 项目的版本更新为 4.2.6 RELEASE。这样做解决了我的问题。
以下是我所做的配置:
我的配置文件:
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Support for Joda Time -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="customJacksonMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
我的 class 扩展了 Object
@Component("customJacksonMapper")
public class CustomJacksonMapper extends ObjectMapper {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* Instantiates a new custom jackson mapper.
*
* RegisterModule = Registar o módulo do JodaTime.
* Locale = Padrão portugues Brasil.
* TimeZone = Converte para o timezone de São Paulo.
*
*/
public CustomJacksonMapper() {
this.registerModule(new JodaModule());
this.setLocale(new Locale("pt_BR"));
this.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS , false);
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
使用此配置,正如我在很多帖子中所读到的,日期应该以 Json 中的正确格式返回,但我得到的是以下内容:
{"date": 1467920285301}
我做错了什么?
尝试将这一行添加到构造函数中:
this.setDateFormat(new ISO8601DateFormat());
我将 Spring 项目的版本更新为 4.2.6 RELEASE。这样做解决了我的问题。