亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問題解決方案

 更新時間:2023年01月02日 09:15:17   作者:code2roc  
這篇文章主要介紹了fastjson通過代碼指定全局序列化返回時間格式,導(dǎo)致使用JSONField注解標(biāo)注屬性的特殊日期返回格式失效問題的解決方案

問題描述

fastjson通過代碼指定全局序列化返回時間格式,導(dǎo)致使用JSONField注解標(biāo)注屬性的特殊日期返回格式失效

使用版本

應(yīng)用名稱版本
springboot2.0.0.RELEASE
fastjson1.2.83

全局設(shè)置代碼

public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.需要定義一個convert轉(zhuǎn)換消息的對象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json數(shù)據(jù);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //全局指定了日期格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //3處理中文亂碼問題
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //5.將convert添加到converters當(dāng)中.
        converters.add(fastJsonHttpMessageConverter);
    }
}

屬性設(shè)置代碼

    @JSONField(format = "yyyy-MM-dd")
    private Date addDate;

返回結(jié)果

請求接口后addDate字段返回的格式為2022-12-17 13:26:45,仍然為全局日期格式,并不設(shè)置JSONField后期望的2022-12-17格式

解決方案

統(tǒng)一掃描

系統(tǒng)啟動時掃描對應(yīng)基類的子類,通過反射獲取標(biāo)注了JSONField注解的字段并獲取對應(yīng)的format值

public class FastJsonUtil {
    private static HashMap<String, String> dateMap = new HashMap();
    public static void scanDate2Json(Class runtimeClass, List<String> extraPackageNameList) {
        List<Class> filterClassList = new ArrayList<>();
        filterClassList.add(BaseSystemObject.class);
        List<Class<?>> scanClassList = new ArrayList<>();
        if (!runtimeClass.getPackage().getName().equals(Application.class.getPackage().getName())) {
            scanClassList.addAll(ScanUtil.getAllClassByPackageName(runtimeClass.getPackage(), filterClassList));
        }
        for (String packageName : extraPackageNameList) {
            scanClassList.addAll(ScanUtil.getAllClassByPackageName(packageName, filterClassList));
        }
        for (Class clazz : scanClassList) {
            List<Field> fs = Arrays.asList(clazz.getDeclaredFields());
            for (Field field : fs) {
                field.setAccessible(true);
                if (field.getType() == Date.class) {
                    JSONField jsonField = field.getAnnotation(JSONField.class);
                    if (jsonField != null && !StringUtil.isEmpty(jsonField.format())) {
                        dateMap.put(clazz.getName() + "|" + field.getName(), jsonField.format());
                    }
                }
            }
        }
    }
    public static boolean checkDate2Json(String key){
        return dateMap.containsKey(key);
    }
    public static String getDate2JsonFormat(String key){
        return dateMap.get(key);
    }
}

統(tǒng)一修改

實(shí)現(xiàn)fastjson的擴(kuò)展過濾器ValueFilter進(jìn)行序列化后的值修改,并注冊到配置中去

public class FastJsonPropertyValueFilter implements ValueFilter {
    @Override
    public Object process(Object source, String name, Object value) {
        String key = source.getClass().getName() + "|" + name;
        if (value != null && FastJsonUtil.checkDate2Json(key)) {
            String format = FastJsonUtil.getDate2JsonFormat(key);
            DateFormat df = new SimpleDateFormat(format);
            return df.format(value);
        }
        return value;
    }
}
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.需要定義一個convert轉(zhuǎn)換消息的對象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json數(shù)據(jù);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //全局指定了日期格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //指定json返回規(guī)則
        fastJsonConfig.setSerializeFilters(new FastJsonPropertyValueFilter());
        //3處理中文亂碼問題
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //5.將convert添加到converters當(dāng)中.
        converters.add(fastJsonHttpMessageConverter);
    }
}

到此這篇關(guān)于fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問題解決方案的文章就介紹到這了,更多相關(guān)fastjson日期序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • File.createTempFile創(chuàng)建臨時文件的示例詳解

    File.createTempFile創(chuàng)建臨時文件的示例詳解

    這篇文章主要介紹了File.createTempFile創(chuàng)建臨時文件的示例詳解,在默認(rèn)臨時文件目錄中創(chuàng)建一個空文件,使用給定前綴和后綴生成其名稱。 如果感興趣來了解一下
    2020-07-07
  • SpringBoot獲取當(dāng)前運(yùn)行環(huán)境三種方式小結(jié)

    SpringBoot獲取當(dāng)前運(yùn)行環(huán)境三種方式小結(jié)

    在使用SpringBoot過程中,我們只需要引入相關(guān)依賴,然后在main方法中調(diào)用SpringBootApplication.run(應(yīng)用程序啟動類.class)方法即可,那么SpringBoot是如何獲取當(dāng)前運(yùn)行環(huán)境呢,接下來由小編給大家介紹一下SpringBoot獲取當(dāng)前運(yùn)行環(huán)境三種方式,需要的朋友可以參考下
    2024-01-01
  • Java如何打印完整的堆棧信息

    Java如何打印完整的堆棧信息

    這篇文章主要為大家介紹了Java如何打印完整的堆棧信息示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • springboot中使用rabbitt的詳細(xì)方法

    springboot中使用rabbitt的詳細(xì)方法

    這篇文章主要介紹了springboot中使用rabbitt,通過本文學(xué)習(xí)讓我們了解如何在Spring Boot中使用RabbitMQ,并使用不同的交換機(jī)和隊列類型以及消息確認(rèn)模式,需要的朋友可以參考下
    2023-05-05
  • java對象初始化順序驗(yàn)證示例

    java對象初始化順序驗(yàn)證示例

    以下這段小程序?qū)φ{(diào)用對象構(gòu)造函數(shù)時,父類構(gòu)造函數(shù)、成員變量初始化函數(shù),以及非靜態(tài)初始化塊調(diào)用順序進(jìn)行驗(yàn)證,不考慮靜態(tài)成員及靜態(tài)初始化塊
    2014-02-02
  • Spring Boot 日志配置方法(超詳細(xì))

    Spring Boot 日志配置方法(超詳細(xì))

    默認(rèn)情況下,Spring Boot會用Logback來記錄日志,并用INFO級別輸出到控制臺。下面通過本文給大家介紹Spring Boot 日志配置方法詳解,感興趣的朋友參考下吧
    2017-07-07
  • Java并發(fā)編程之ConcurrentLinkedQueue隊列詳情

    Java并發(fā)編程之ConcurrentLinkedQueue隊列詳情

    這篇文章主要介紹了Java并發(fā)編程之ConcurrentLinkedQueue隊列詳情,ConcurrentLinkedQueue?內(nèi)部的隊列使用單向鏈表方式實(shí)現(xiàn),下文更多相關(guān)內(nèi)容敘述需要的小伙伴可以參考一下
    2022-04-04
  • MybatisPlus字段自動填充失效,填充值為null的解決方案

    MybatisPlus字段自動填充失效,填充值為null的解決方案

    這篇文章主要介紹了MybatisPlus字段自動填充失效,填充值為null的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java實(shí)現(xiàn)彈窗效果的基本操作

    Java實(shí)現(xiàn)彈窗效果的基本操作

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)彈窗效果的基本操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇

    詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇

    這篇文章主要介紹了詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論