8
SpringMVC配置
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<list>
<value>AllowArbitraryCommas</value>
<value>AllowUnQuotedFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
<property name="dateFormat" value="yyyy-MM-dd"></property>
<property name="serializerFeatures">
<list>
<value>QuoteFieldNames</value>
<value>WriteMapNullValue</value>
<value>WriteNullListAsEmpty</value>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</property>
</bean>
实体:
public class AccountExt {
@JSONField (format="yyyy-MM-dd HH:mm:ss")
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
跟踪源码 com.alibaba.fastjson.serializer.JSONSerializer类中方法
public final void writeWithFormat(Object object, String format) {
if(object instanceof Date) {
// 获取xml配置的时间格式,不为空,为yyyy-MM-dd
// 传入的format(实体类中配置的yyyy-MM-dd HH:mm:ss),无法使用
DateFormat dateFormat = this.getDateFormat();
if(dateFormat == null) {
dateFormat = new SimpleDateFormat(format, this.locale);
((DateFormat)dateFormat).setTimeZone(this.timeZone);
}
String text = ((DateFormat)dateFormat).format((Date)object);
this.out.writeString(text);
}
......
}
使用版本先后从1.2.24切换到1.2.33
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>
请求帮助,是否我有配置不当的地方,导致无法使用实体类中的时间格式。万分感谢!