springboot實(shí)現(xiàn)FastJson解析json數(shù)據(jù)的方法
最近在研究springboot實(shí)現(xiàn)FastJson解析json數(shù)據(jù)的方法,那么今天也算個(gè)學(xué)習(xí)筆記吧!
添加jar包:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
兩種方式啟動(dòng)加載類(lèi):
第一種繼承WebMvcConfigurerAdapter,重寫(xiě)configureMessageConverters方法:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class App extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(App.class, args); } @Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { // TODO Auto-generated method stub super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat ); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter); } }
第二種方式bean注入HttpMessageConverters:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.converter.HttpMessageConverter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class AppTwo{ public static void main(String[] args) { SpringApplication.run(AppTwo.class, args); } @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }
最后屬性前加@JSONField:
@JSONField(serialize=false) private Long id;
返回前端就會(huì)沒(méi)有id這個(gè)屬性值
以上就是本文的全部?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)
- Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
- Spring?boot詳解fastjson過(guò)濾字段為null值如何解決
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之二叉排序樹(shù)的實(shí)現(xiàn)
二叉排序樹(shù)(Binary Sort Tree),又稱(chēng)二叉查找樹(shù)(Binary Search Tree),亦稱(chēng)二叉搜索樹(shù)。本文詳細(xì)介紹了二叉排序樹(shù)的原理,并且提供了Java代碼的完全實(shí)現(xiàn)。需要的可以參考一下2022-01-01Springboot項(xiàng)目使用html5的video標(biāo)簽完成視頻播放功能
這篇文章主要介紹了Springboot項(xiàng)目使用html5的video標(biāo)簽完成視頻播放功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12解決SpringCloud Feign傳對(duì)象參數(shù)調(diào)用失敗的問(wèn)題
這篇文章主要介紹了解決SpringCloud Feign傳對(duì)象參數(shù)調(diào)用失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring Data JPA踩坑記錄(@id @GeneratedValue)
這篇文章主要介紹了Spring Data JPA踩坑記錄(@id @GeneratedValue),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07詳解Java TCC分布式事務(wù)實(shí)現(xiàn)原理
這篇文章主要介紹了詳解Java TCC分布式事務(wù)實(shí)現(xiàn)原理,對(duì)分布式事務(wù)感興趣的同學(xué),一定要看一下2021-04-04Java單例模式下的MongoDB數(shù)據(jù)庫(kù)操作工具類(lèi)
這篇文章主要介紹了Java單例模式下的MongoDB數(shù)據(jù)庫(kù)操作工具類(lèi),結(jié)合實(shí)例形式分析了java基于單例模式下操作MongoDB數(shù)據(jù)庫(kù)相關(guān)連接、查詢(xún)、插入、刪除等操作封裝技巧,需要的朋友可以參考下2018-01-01Java中的對(duì)象、類(lèi)、抽象類(lèi)、接口、繼承之間的聯(lián)系
這篇文章主要介紹了Java中的對(duì)象、類(lèi)、抽象類(lèi)、接口、繼承之間的聯(lián)系,文章講解的很清晰,有不太懂的同學(xué)可以多研究下2021-02-02Java數(shù)據(jù)結(jié)構(gòu)--時(shí)間和空間復(fù)雜度
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)的時(shí)間和空間復(fù)雜度,小編覺(jué)得這篇文寫(xiě)的不錯(cuò),感興趣的朋友可以了解下,希望能夠給你帶來(lái)幫助2021-08-08Java?empty、null、blank的區(qū)別小結(jié)
本文主要介紹了Java?empty、null、blank的區(qū)別小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06