Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
個人使用比較習慣的json框架是fastjson,所以spring boot默認的json使用起來就很陌生了,所以很自然我就想我能不能使用fastjson進行json解析呢?
1.引入fastjson依賴庫:
<!--添加fastjson解析JSON數(shù)據(jù)--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.16</version> </dependency>
2.配置fastjson
這里要說下很重要的話,官方文檔說的1.2.10以后,會有兩個方法支持HttpMessageconvert,一個是FastJsonHttpMessageConverter,支持4.2以下的版本,一個是FastJsonHttpMessageConverter4支持4.2以上的版本,具體有什么區(qū)別暫時沒有深入研究。這里也就是說:低版本的就不支持了,所以這里最低要求就是1.2.10+
方式一:
(1)啟動類繼承WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters
具體代碼:
@SpringBootApplication // 申明讓spring boot自動給程序進行必要的配置,等價于以默認屬性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
public class Application extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 初始化轉(zhuǎn)換器
FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
// 初始化一個轉(zhuǎn)換器配置
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 將配置設置給轉(zhuǎn)換器并添加到HttpMessageConverter轉(zhuǎn)換器列表中
fastConvert.setFastJsonConfig(fastJsonConfig);
converters.add(fastConvert);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
方式二:
在配置類或啟動類中,注入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);
}
}
配置完成后,在實體類中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的話,那么恭喜你配置成功了,其中JSONField的包路徑是:com.alibaba.fastjson.annotation.JSONField
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 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)
- springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法
- Spring?boot詳解fastjson過濾字段為null值如何解決
相關文章
MyBatis中如何查詢某個時間段內(nèi)的數(shù)據(jù)
這篇文章主要介紹了MyBatis中如何查詢某個時間段內(nèi)的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
java實現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具
這篇文章主要為大家詳細介紹了java實現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
maven?scope?provided和runtime的例子說明
這篇文章主要介紹了maven?scope?provided和runtime的例子說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Security框架:如何使用CorsFilter解決前端跨域請求問題
這篇文章主要介紹了Security框架:如何使用CorsFilter解決前端跨域請求問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java使用數(shù)組實現(xiàn)ArrayList的動態(tài)擴容的方法
這篇文章主要介紹了Java使用數(shù)組實現(xiàn)ArrayList的動態(tài)擴容的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

