SpringBoot整合Gson 整合Fastjson的實例詳解
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實體
/**
* @Create_Author msfh
* @Create_Date 2020-11-25 15:54:15
* @Description User實體
*/
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 # 我們可以分別寫兩個bean去實現(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依賴
# 這個沒什么好說的,還是移除自帶的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實體(上邊代碼)
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實現(xiàn)Github的OAuth第三方登錄
- SpringBoot整合FastJson過程解析
- SpringBoot Redis配置Fastjson進行序列化和反序列化實現(xiàn)
- springboot實現(xiàn)FastJson解析json數(shù)據(jù)的方法
- Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
- Spring?boot詳解fastjson過濾字段為null值如何解決
相關(guān)文章
springboot整合sentinel接口熔斷的實現(xiàn)示例
為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運行,本文介紹了如何使用Spring Boot與Sentinel進行接口熔斷的配置與實現(xiàn),感興趣的可以了解一下2024-09-09
SpringBoot中GlobalExceptionHandler異常處理機制詳細說明
Spring Boot的GlobalExceptionHandler是一個全局異常處理器,用于捕獲和處理應(yīng)用程序中發(fā)生的所有異常,這篇文章主要給大家介紹了關(guān)于Java中GlobalExceptionHandler異常處理機制的相關(guān)資料,需要的朋友可以參考下2024-03-03
詳解Spring Security中權(quán)限注解的使用
這篇文章主要為大家詳細介紹一下Spring Security中權(quán)限注解的使用方法,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定參考價值,需要的可以參考一下2022-05-05
linux中nohup?java?-jar啟動java項目的步驟
nohup是一個Unix和Linux命令,用于運行關(guān)閉時不會被終止的進程,這篇文章主要給大家介紹了關(guān)于linux中nohup?java?-jar啟動java項目的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-08-08

