Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
個(gè)人使用比較習(xí)慣的json框架是fastjson,所以spring boot默認(rèn)的json使用起來(lái)就很陌生了,所以很自然我就想我能不能使用fastjson進(jìn)行json解析呢?
1.引入fastjson依賴(lài)庫(kù):
<!--添加fastjson解析JSON數(shù)據(jù)--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.16</version> </dependency>
2.配置fastjson
這里要說(shuō)下很重要的話(huà),官方文檔說(shuō)的1.2.10以后,會(huì)有兩個(gè)方法支持HttpMessageconvert,一個(gè)是FastJsonHttpMessageConverter,支持4.2以下的版本,一個(gè)是FastJsonHttpMessageConverter4支持4.2以上的版本,具體有什么區(qū)別暫時(shí)沒(méi)有深入研究。這里也就是說(shuō):低版本的就不支持了,所以這里最低要求就是1.2.10+
方式一:
(1)啟動(dòng)類(lèi)繼承WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters
具體代碼:
@SpringBootApplication // 申明讓spring boot自動(dòng)給程序進(jìn)行必要的配置,等價(jià)于以默認(rèn)屬性使用@Configuration,@EnableAutoConfiguration和@ComponentScan public class Application extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); // 初始化轉(zhuǎn)換器 FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter(); // 初始化一個(gè)轉(zhuǎn)換器配置 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 將配置設(shè)置給轉(zhuǎn)換器并添加到HttpMessageConverter轉(zhuǎn)換器列表中 fastConvert.setFastJsonConfig(fastJsonConfig); converters.add(fastConvert); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
方式二:
在配置類(lèi)或啟動(dòng)類(lèi)中,注入Bean : HttpMessageConverters
/** * Bean配置管理 * Created by surpass.wei@gmail.com on 2017/2/21. */ @Configuration public class BeanConfig { /*注入Bean : HttpMessageConverters,以支持fastjson*/ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConvert.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters((HttpMessageConverter<?>) fastConvert); } }
配置完成后,在實(shí)體類(lèi)中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的話(huà),那么恭喜你配置成功了,其中JSONField的包路徑是:com.alibaba.fastjson.annotation.JSONField
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot中使用FastJson解決long類(lèi)型在js中失去精度的問(wèn)題
- SpringBoot整合Gson 整合Fastjson的實(shí)例詳解
- SpringBoot如何使用Fastjson解析Json數(shù)據(jù)
- springboot中用fastjson處理返回值為null的屬性值
- 使用SpringBoot+OkHttp+fastjson實(shí)現(xiàn)Github的OAuth第三方登錄
- SpringBoot整合FastJson過(guò)程解析
- SpringBoot Redis配置Fastjson進(jìn)行序列化和反序列化實(shí)現(xiàn)
- springboot實(shí)現(xiàn)FastJson解析json數(shù)據(jù)的方法
- Spring?boot詳解fastjson過(guò)濾字段為null值如何解決
相關(guān)文章
Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例
這篇文章主要介紹了Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分,涉及java字符串的遍歷、比較、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09Java基本數(shù)據(jù)類(lèi)型和運(yùn)算符詳解
這篇文章主要介紹了Java基本數(shù)據(jù)類(lèi)型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類(lèi)型、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下2020-02-02springboot配置templates直接訪問(wèn)的實(shí)現(xiàn)
這篇文章主要介紹了springboot配置templates直接訪問(wèn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12MyBatis-Plus 自動(dòng)填充的實(shí)現(xiàn)示例
MyBatis-Plus 提供了自動(dòng)填充功能,幫助開(kāi)發(fā)者在插入或更新數(shù)據(jù)時(shí),自動(dòng)為某些字段賦值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09Javaweb使用cors完成跨域ajax數(shù)據(jù)交互
本文由跨域、cors的概念開(kāi)始,進(jìn)而向大家介紹了Javaweb使用cors完成跨域ajax數(shù)據(jù)交互的相關(guān)內(nèi)容,需要的朋友可以了解下。2017-09-09java 中HashMap實(shí)現(xiàn)原理深入理解
這篇文章主要介紹了java 中HashMap實(shí)現(xiàn)原理深入理解的相關(guān)資料,需要的朋友可以參考下2017-03-03SpringBoot源碼分析之bootstrap.properties文件加載的原理
本文通過(guò)訪問(wèn)看到bootstrap.properties中的信息獲取到了,同時(shí)age也被application.properties中的屬性覆蓋掉了。加載順序到底是什么?為什么會(huì)覆蓋呢?我們接下來(lái)分析下吧2021-12-12