<bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="user" class="zxy.demo.springmvc.domain.UserInfo"> <constructor-arg value="10" /> <constructor-arg index="1" value="名字" /> <constructor-arg index="2"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2016-12-19" /> </bean> </constructor-arg> </bean>
做用于单个实体,用于xml文件配置。html
这是一个注解,完整类名是org.springframework.format.annotation.DateTimeFormat
,用于http请求入参
,只能做用于具体的实体对象,以下java
@DateTimeFormat(pattern="yyyy-MM-dd")
只用于http请求入参
,做用于全局,可搭配@Controller
和@ControllerAdvice
使用,以下web
@ControllerAdvice public class MyControllerAdvice { @InitBinder public void initDate(WebDataBinder binder) { // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH"); // dateFormat.setLenient(false); // binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd")); // Spring 4.2以后的写法 } }
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="zxy.demo.springmvc.converter.DateConverter"/> </set> </property> </bean> <mvc:annotation-driven conversion-service="conversionService"/>
public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { // ... } }
DateConverter
是一个自定义类,实现接口org.springframework.core.convert.converter.Converter
,重写convert()方法便可。做用于全局,用于http请求入参
。spring
前面说的日期处理,没有一种是用于请求返回
的,若是是要返回数据,而且使用json进行系列化的,那么SpringMVC支持的有jackson跟Gson,具体要看引入了哪一个jar包,若是两个引入了,那么将以jackson为准,具体可看org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
这个类,关键看两个成员变量jackson2Present
跟gsonPresent
,这是两个boolean值,根据这两个成员变量看关联的代码便可。json
注:因为通常接口返回的数据是json格式的,因此jackson跟Gson这两个包确定是要引入一个的,否则请求会报错,如“org.springframework.core.convert.ConversionFailedException: Failed to convert from type xxx to type xxx for ...”,这时候引入两个包之中的一个就能够让对象序列化为Json进行传输了。同理xml格式的序列化也可参考AnnotationDrivenBeanDefinitionParser
这个类进行相应的配置。mvc
注:下面的代码是基于Spring 4.3.8.RELEASE,其余版本应该也相似。app
这是一个注解,完整类名是com.fasterxml.jackson.annotation.JsonFormat
,只能用于http请求返回
,只能做用于具体的实体对象,以下dom
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
做用于全局,只用于请求返回
。ide
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper" > <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:simpleDateFormat="yyyy-MM-dd HH:mm:ss" /> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
做用于全局,只用于请求返回
。code
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"> <property name="gson"> <bean class="org.springframework.http.converter.json.GsonFactoryBean" p:dateFormatPattern="yyyy-MM-dd HH:mm:ss" /> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
若是做用域单个的跟做用于全局的都配置了,那么配置单个实体的将最终生效而不会用全局的配置。
对于使用message-converters
的方式,若是还须要更多的配置参数,能够看spring-webmvc的AnnotationDrivenBeanDefinitionParser
这个类跟spring-web的org.springframework.http.converter.json
包下的类便可。
更多的日期时间处理方式,可参考官方文档。