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