1
這裏是我所做的配置:配置不工作於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>
我的類,它擴展對象
@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}
我在做什麼錯?
不是。它不支持這種配置 – Gabriel
您能詳細說明嗎?日期是否仍以同樣的方式序列化?另外,你可以提供你使用這個自定義映射器的代碼嗎? – mdziob
是的日期以相同的方式進行序列化。這個自定義映射器在春天使用,我只是覆蓋了Spring的ObjectMapper以使用CustomJacksonMapper – Gabriel