springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法
最近在研究springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法,那么今天也算個學(xué)習(xí)筆記吧!
添加jar包:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
兩種方式啟動加載類:
第一種繼承WebMvcConfigurerAdapter,重寫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;
返回前端就會沒有id這個屬性值
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot中使用FastJson解決long類型在js中失去精度的問題
- SpringBoot整合Gson 整合Fastjson的實例詳解
- SpringBoot如何使用Fastjson解析Json數(shù)據(jù)
- springboot中用fastjson處理返回值為null的屬性值
- 使用SpringBoot+OkHttp+fastjson實現(xiàn)Github的OAuth第三方登錄
- SpringBoot整合FastJson過程解析
- SpringBoot Redis配置Fastjson進行序列化和反序列化實現(xiàn)
- Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
- Spring?boot詳解fastjson過濾字段為null值如何解決
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的實現(xiàn)
二叉排序樹(Binary Sort Tree),又稱二叉查找樹(Binary Search Tree),亦稱二叉搜索樹。本文詳細介紹了二叉排序樹的原理,并且提供了Java代碼的完全實現(xiàn)。需要的可以參考一下2022-01-01Springboot項目使用html5的video標簽完成視頻播放功能
這篇文章主要介紹了Springboot項目使用html5的video標簽完成視頻播放功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12解決SpringCloud Feign傳對象參數(shù)調(diào)用失敗的問題
這篇文章主要介紹了解決SpringCloud Feign傳對象參數(shù)調(diào)用失敗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Spring Data JPA踩坑記錄(@id @GeneratedValue)
這篇文章主要介紹了Spring Data JPA踩坑記錄(@id @GeneratedValue),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Java單例模式下的MongoDB數(shù)據(jù)庫操作工具類
這篇文章主要介紹了Java單例模式下的MongoDB數(shù)據(jù)庫操作工具類,結(jié)合實例形式分析了java基于單例模式下操作MongoDB數(shù)據(jù)庫相關(guān)連接、查詢、插入、刪除等操作封裝技巧,需要的朋友可以參考下2018-01-01Java中的對象、類、抽象類、接口、繼承之間的聯(lián)系
這篇文章主要介紹了Java中的對象、類、抽象類、接口、繼承之間的聯(lián)系,文章講解的很清晰,有不太懂的同學(xué)可以多研究下2021-02-02Java數(shù)據(jù)結(jié)構(gòu)--時間和空間復(fù)雜度
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)的時間和空間復(fù)雜度,小編覺得這篇文寫的不錯,感興趣的朋友可以了解下,希望能夠給你帶來幫助2021-08-08Java?empty、null、blank的區(qū)別小結(jié)
本文主要介紹了Java?empty、null、blank的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06