SpringBoot整合Gson 整合Fastjson的實(shí)例詳解
SpringBoot整合Gson 整合Fastjson
一、SpringBoot整合Gson
1、pom依賴
# 在SpringBoot中給我們自帶了json解析器,我們需要移除SpringBoot自帶的jackson,在添加Gson依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--移除jackson依賴--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <!--添加Gson依賴--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、User實(shí)體
/** * @Create_Author msfh * @Create_Date 2020-11-25 15:54:15 * @Description User實(shí)體 */ public class User { private Integer id; private String name; private Date birthday; @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + '}'; } /**省略set&get**/ }
3、UserController
/** * @Create_Author msfh * @Create_Date 2020-11-25 15:55:15 * @Description UserController控制器 */ @RestController public class UserController { @GetMapping("/user") public List<User> getUsers(){ ArrayList<User> users = new ArrayList<>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setId(i); user.setName("msfh-->"+i); user.setBirthday(new Date()); users.add(user); } return users; } }
4、WebMvcConfig
# 在之前的一篇博客中有介紹,大家不太明白可以先看一下上一篇博客,這次就不放測試的結(jié)果了! # 在GsonHttpMessageConvertersConfiguration中含有GsonHttpMessageConverter # 在GsonAutoConfiguration中含有Gson # 我們可以分別寫兩個(gè)bean去實(shí)現(xiàn)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").create()); // return converter; //} @Bean Gson gson(){ return new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); } }
二、SpringBoot整合FastJson
1、pom依賴
# 這個(gè)沒什么好說的,還是移除自帶的Jackson,添加fastjson,不再做過多解釋
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--移除jackson依賴--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <!--添加fastjson依賴--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.74</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、User實(shí)體(上邊代碼)
3、UserController(上邊代碼)
4、WebMvcConfig
# 在fastjson中稍微和之前兩種不一致
# 在FastJsonHttpMessageConverter找到FastJsonConfig看一下
/** * @Create_Author msfh * @Create_Date 2020-11-25 16:05:56 * @Description WebMvcConfig配置類 */ @Configuration public class WebMvcConfig { @Bean FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){ FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy/MM/dd"); converter.setFastJsonConfig(config); return converter; } }
5、測試(無問題)
到此這篇關(guān)于SpringBoot整合Gson 整合Fastjson的文章就介紹到這了,更多相關(guān)SpringBoot整合Gson 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot中使用FastJson解決long類型在js中失去精度的問題
- SpringBoot如何使用Fastjson解析Json數(shù)據(jù)
- springboot中用fastjson處理返回值為null的屬性值
- 使用SpringBoot+OkHttp+fastjson實(shí)現(xiàn)Github的OAuth第三方登錄
- SpringBoot整合FastJson過程解析
- SpringBoot Redis配置Fastjson進(jìn)行序列化和反序列化實(shí)現(xiàn)
- springboot實(shí)現(xiàn)FastJson解析json數(shù)據(jù)的方法
- Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
- Spring?boot詳解fastjson過濾字段為null值如何解決
相關(guān)文章
Java如何使用spire進(jìn)行word文檔的替換詳解
創(chuàng)作一份文案經(jīng)常會(huì)高頻率地使用某些詞匯,如地名、人名、人物職位等,若表述有誤,就需要整體撤換,下面這篇文章主要給大家介紹了關(guān)于Java如何使用spire進(jìn)行word文檔的替換的相關(guān)資料,需要的朋友可以參考下2023-01-01springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例
為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運(yùn)行,本文介紹了如何使用Spring Boot與Sentinel進(jìn)行接口熔斷的配置與實(shí)現(xiàn),感興趣的可以了解一下2024-09-09SpringBoot中GlobalExceptionHandler異常處理機(jī)制詳細(xì)說明
Spring Boot的GlobalExceptionHandler是一個(gè)全局異常處理器,用于捕獲和處理應(yīng)用程序中發(fā)生的所有異常,這篇文章主要給大家介紹了關(guān)于Java中GlobalExceptionHandler異常處理機(jī)制的相關(guān)資料,需要的朋友可以參考下2024-03-03詳解Spring Security中權(quán)限注解的使用
這篇文章主要為大家詳細(xì)介紹一下Spring Security中權(quán)限注解的使用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以參考一下2022-05-05linux中nohup?java?-jar啟動(dòng)java項(xiàng)目的步驟
nohup是一個(gè)Unix和Linux命令,用于運(yùn)行關(guān)閉時(shí)不會(huì)被終止的進(jìn)程,這篇文章主要給大家介紹了關(guān)于linux中nohup?java?-jar啟動(dòng)java項(xiàng)目的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08