1,通用处理
每个ajax返回的日期类型的数据都会按照这个格式处理
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="jacksonMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper" ref="objectMapper" />
</bean>
<!-- 日期的格式化 -->
<bean id="fullDateTime" class="java.text.SimpleDateFormat">
<constructor-arg>
<value>yyyy-MM-dd HH:mm:ss</value>
</constructor-arg>
</bean>
<!-- 对象类型的输出,被MySpringContextHolder引用-->
<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<ref bean="fullDateTime" />
</property>
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
2,个别特殊处理
如果有日期类型的格式与通用类型不一样,可以在bean的get方法中增加如下定义
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
public Date getRegDate() {
return regDate;
}