SpringMVC配制全局的日期格式

时间:2022-05-24 03:49:40

SpringMVC文件配制:

<mvc:annotation-driven>
<!-- 处理responseBody 里面日期类型 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

特殊日期格式配制使用(@JsonFormat)在get方法上,如下:

@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
public Date getBirth() {
return birth;
}