springmvc fastjson 反序列化時間格式化方法(推薦)
第一種情況是從后臺拿到數(shù)據(jù),進(jìn)行反序列化,反序列化格式時間:試了一下很多網(wǎng)上的方法,最后發(fā)現(xiàn)還是在實(shí)體類上面的日期字段加上如下注解,可以完成格式化操作,否則默認(rèn)就都是時間戳的格式:
@JSONField (format="yyyy-MM-dd HH:mm:ss")
public Date birthday;
@JSONField (format="yyyy-MM-dd HH:mm:ss")
public Date birthday;
第二種情況是:response返回給前段的時間格式,一開始是時間戳,需要轉(zhuǎn)成想要的格式y(tǒng)yyy-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);
}
}
然后,將springMVC.xml(具體文件名以項(xiàng)目而定) 的配置修改為如下, 引用重寫了writeInternal()方法的類進(jìn)行json序列化
<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 反序列化時間格式化方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)停車場管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)停車場管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
解決redisTemplate向redis中插入String類型數(shù)據(jù)時出現(xiàn)亂碼問題
這篇文章主要介紹了解決redisTemplate向redis中插入String類型數(shù)據(jù)時出現(xiàn)亂碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
org.apache.ibatis.annotations不存在的問題
這篇文章主要介紹了org.apache.ibatis.annotations不存在的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
如何利用grep-console插件使Intellij idea顯示多顏色調(diào)試日志
這篇文章主要介紹了利用grep-console插件使Intellij idea顯示多顏色調(diào)試日志,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

