介绍
这篇文章将为大家详细讲解有关使用SpringBoot怎么对Fastjson于Gson进行整合,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
,一,SpringBoot整合Gson
1, pom依赖
#在SpringBoot中给我们自带了json解析器,我们需要移除SpringBoot自带的杰克逊,在添加Gson依赖
& lt; dependencies> ,& lt; dependency> & lt;才能groupId> org.springframework.boot</groupId> & lt;才能artifactId> spring-boot-starter-web</artifactId> & lt;才能!——移除杰克逊依赖——比; & lt;才能exclusions> ,,& lt; exclusion> ,,,& lt; groupId> org.springframework.boot ,,,& lt; artifactId> spring-boot-starter-json ,,& lt;/exclusion> & lt;才能/exclusions> ,& lt;/dependency> ,& lt; !——添加Gson依赖——比; ,& lt; dependency> & lt;才能groupId> com.google.code.gson</groupId> & lt;才能artifactId> gson</artifactId> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> org.springframework.boot</groupId> & lt;才能artifactId> spring-boot-starter-test</artifactId> & lt;才能scope> test</scope> ,& lt;/dependency> & lt;/dependencies>
2,用户实体
/* * ,* @Create_Author msfh ,* @Create_Date 2020 - 11 - 25, 15:54:15 ,* @Description 用户实体 ,*/public class  User { ,private Integer id; ,private String 名称; 生日,private Date ; ,@Override ,public String  toString (), { return “用户才能{“+ ,,,“id=? +, id + ,,,,,,的名字=& # 39;“,+,name +, & # 39; \ & # 39; & # 39; + ,,,,,,生日=?+,birthday + ,,,& # 39;}& # 39;; ,} ,/* *省略set& * */}
3,用户控件
/* * ,* @Create_Author msfh ,* @Create_Date 2020 - 11 - 25, 15:55:15 ,* @Description 用户控件控制器 ,*/@RestController public class  UserController { ,@GetMapping (“/user") ,public Listgetuser () { ArrayList<才能;User>, users =, new ArrayList<在(); for 才能;(int 小姐:=,0;,小姐:& lt;, 10;,我+ +),{ ,,User User =, new 用户(); ,,user.setId(我); ,,user.setName (“msfh——在“+ i); ,,user.setBirthday (new 日期()); ,,users.add(用户); ,,} return 才能;用户; ,} }
4, WebMvcConfig
#,在之前的一篇博客中有介绍,大家不太明白可以先看一下上一篇博客,这次就不放测试的结果了! #,在GsonHttpMessageConvertersConfiguration中含有GsonHttpMessageConverter #,在GsonAutoConfiguration中含有Gson #,我们可以分别写两个bean去实现gson的配置(GsonHttpMessageConverter或gson) #,建议大家没事的话,可以看下源码
/* * ,* @Create_Author msfh ,* @Create_Date 2020 - 11 - 25, 16:05:56 ,* @Description WebMvcConfig配置类 ,*/@ configuration public class  WebMvcConfig {//@ bean ,//GsonHttpMessageConverter gsonHttpMessageConverter () { ,//GsonHttpMessageConverter converter =, new GsonHttpMessageConverter (); ,//converter.setGson (new GsonBuilder () .setDateFormat (“yyyy-MM-dd")共创()); ,//return 变频器; ,//} ,@ bean ,Gson gson () { return 才能;new  GsonBuilder () .setDateFormat (“yyyy-MM-dd")共创(); ,} }
二,SpringBoot整合FastJson
1, pom依赖
#这个没什么好说的,还是移除自带的杰克逊,添加FastJson,不再做过多解释
& lt; dependencies> ,& lt; dependency> & lt;才能groupId> org.springframework.boot</groupId> & lt;才能artifactId> spring-boot-starter-web</artifactId> & lt;才能!——移除杰克逊依赖——比; & lt;才能exclusions> ,,& lt; exclusion> ,,,& lt; groupId> org.springframework.boot ,,,& lt; artifactId> spring-boot-starter-json ,,& lt;/exclusion> & lt;才能/exclusions> ,& lt;/dependency> ,& lt; !——添加fastjson依赖——比; ,& lt; dependency> & lt;才能groupId> com.alibaba</groupId> & lt;才能artifactId> fastjson</artifactId> & lt;才能version> 1.2.74</version> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> org.springframework.boot</groupId> & lt;才能artifactId> spring-boot-starter-test</artifactId> & lt;才能scope> test</scope> ,& lt;/dependency> null null null null null null null null null null null null null null null null null null使用SpringBoot怎么对Fastjson于Gson进行整合