springmvc fastjson 反序列化时间格式化
2017-03-28 13:56
281 查看
第一种情况是从后台拿到数据,进行反序列化,反序列化格式时间:试了一下很多网上的方法,最后发现还是在实体类上面的日期字段加上如下注解,可以完成格式化操作,否则默认就都是时间戳的格式:
第二种情况是:response返回给前段的时间格式,一开始是时间戳,需要转成想要的格式yyyy-MM-dd重写方法:
[java] view
plain copy
package com.jjs.util;
import java.io.IOException;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
public class JsonHttpMessageConverter extends FastJsonHttpMessageConverter {
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
// TODO Auto-generated method stub
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH";
JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
super.writeInternal(obj, outputMessage);
}
}
然后,将springMVC.xml(具体文件名以项目而定) 的配置修改为如下, 引用重写了writeInternal()方法的类进行json序列化
[html] view
plain copy
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> -->
<bean class="com.jjs.util.JsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<value>WriteDateUseDateFormat</value>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
记录一下,方便查看
@JSONField (format="yyyy-MM-dd HH:mm:ss") public Date birthday; @JSONField (format="yyyy-MM-dd HH:mm:ss") public Date birthday;
第二种情况是:response返回给前段的时间格式,一开始是时间戳,需要转成想要的格式yyyy-MM-dd重写方法:
[java] view
plain copy
package com.jjs.util;
import java.io.IOException;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
public class JsonHttpMessageConverter extends FastJsonHttpMessageConverter {
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
// TODO Auto-generated method stub
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH";
JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
super.writeInternal(obj, outputMessage);
}
}
然后,将springMVC.xml(具体文件名以项目而定) 的配置修改为如下, 引用重写了writeInternal()方法的类进行json序列化
[html] view
plain copy
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> -->
<bean class="com.jjs.util.JsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<value>WriteDateUseDateFormat</value>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
记录一下,方便查看
相关文章推荐
- springmvc fastjson 反序列化时间格式化方法(推荐)
- springmvc访问返回数据类型格式化json报http status 406 和json时间格式化
- fastjson格式化日期 时间
- SpringMVC返回的json数据Date(时间)格式化的问题
- springmvc 中使用fastjson 反序列化json,导致时区相差的问题
- SpringMVC 返回Java8 时间JSON数据的格式化问题处理
- mybatis+springmvc时间格式化,jsp实现或者在ResponseBody日期类型Json处理
- SpringMVC 返回Java8 时间JSON数据的格式化问题处理
- 解决SpringMVC 返回Java8 时间JSON数据的格式化问题处理
- 使用FASTJSON做反序列化的时间格式处理
- Newtonsoft.Json序列化 对时间格式化处理
- SpringMVC格式化显示(时间String)
- ajax异步获得json对象后格式化时间
- SpringMVC杂记(1) 使用阿里巴巴的fastjson
- net.sf.json 日期 时间 格式化 方法
- JSON序列化和反序列化日期时间的处理
- Newtonsoft 和 FastJson 的序列化和反序列化性能的对比
- json时间数据,使用javascript格式化字符串
- LINQ关联表查询语法和.NET扩展方法和JSON.NET时间格式化代码段
- 【第28篇】通过Gson和FastJson格式化Json数据