springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法
更新時間:2017年04月28日 15:34:43 作者:ZhangJQKb
本篇文章主要介紹了springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法,非常具有實用價值,需要的朋友可以參考下
最近在研究springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法,那么今天也算個學習筆記吧!
添加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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 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值如何解決
相關文章
解析ConcurrentHashMap: 預熱(內(nèi)部一些小方法分析)
ConcurrentHashMap是由Segment數(shù)組結構和HashEntry數(shù)組結構組成。Segment的結構和HashMap類似,是一種數(shù)組和鏈表結構,今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧2021-06-06
Spring Boot動態(tài)加載Jar包與動態(tài)配置實現(xiàn)
隨著項目的不斷演進和業(yè)務需求的增長,很多場景下需要實現(xiàn)系統(tǒng)的動態(tài)性和靈活性,本文主要介紹了Spring Boot動態(tài)加載Jar包與動態(tài)配置實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-02-02

