springmvcfastjson反序列化时间格式化方法(推荐)-创新互联
第一种情况是从后台拿到数据,进行反序列化,反序列化格式时间:试了一下很多网上的方法,最后发现还是在实体类上面的日期字段加上如下注解,可以完成格式化操作,否则默认就都是时间戳的格式:
@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重写方法:
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); } }
分享题目:springmvcfastjson反序列化时间格式化方法(推荐)-创新互联
URL网址:http://tyjierui.cn/article/dpseih.html