欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Gson 整合Fastjson的實(shí)例詳解

 更新時(shí)間:2020年11月26日 14:45:03   作者:msfh  
這篇文章主要介紹了SpringBoot整合Gson 整合Fastjson的實(shí)例詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解SpringBoot自定義配置與整合Druid

    詳解SpringBoot自定義配置與整合Druid

    Druid是alibaba開源平臺上一個(gè)數(shù)據(jù)庫連接池實(shí)現(xiàn),結(jié)合了C3P0,DBCP等DB池的優(yōu)點(diǎn),同時(shí)也有Web監(jiān)控界面。這篇文章主要介紹了Java之SpringBoot自定義配置與整合Druid的相關(guān)知識,需要的朋友可以參考下
    2021-09-09
  • java登錄驗(yàn)證碼實(shí)現(xiàn)代碼

    java登錄驗(yàn)證碼實(shí)現(xiàn)代碼

    這篇文章介紹了java實(shí)現(xiàn)登錄驗(yàn)證碼:用興趣的同學(xué)可以參考一下
    2013-10-10
  • Java如何使用spire進(jìn)行word文檔的替換詳解

    Java如何使用spire進(jìn)行word文檔的替換詳解

    創(chuàng)作一份文案經(jīng)常會(huì)高頻率地使用某些詞匯,如地名、人名、人物職位等,若表述有誤,就需要整體撤換,下面這篇文章主要給大家介紹了關(guān)于Java如何使用spire進(jìn)行word文檔的替換的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 消息中間件詳解以及比較選擇

    消息中間件詳解以及比較選擇

    這篇文章主要介紹了消息中間件詳解以及比較選擇,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例

    springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例

    為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運(yùn)行,本文介紹了如何使用Spring Boot與Sentinel進(jìn)行接口熔斷的配置與實(shí)現(xiàn),感興趣的可以了解一下
    2024-09-09
  • SpringBoot中GlobalExceptionHandler異常處理機(jī)制詳細(xì)說明

    SpringBoot中GlobalExceptionHandler異常處理機(jī)制詳細(xì)說明

    Spring Boot的GlobalExceptionHandler是一個(gè)全局異常處理器,用于捕獲和處理應(yīng)用程序中發(fā)生的所有異常,這篇文章主要給大家介紹了關(guān)于Java中GlobalExceptionHandler異常處理機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • 詳解Spring Security中權(quán)限注解的使用

    詳解Spring Security中權(quán)限注解的使用

    這篇文章主要為大家詳細(xì)介紹一下Spring Security中權(quán)限注解的使用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以參考一下
    2022-05-05
  • Java Web十條開發(fā)實(shí)用小知識

    Java Web十條開發(fā)實(shí)用小知識

    這篇文章主要介紹了Java Web十條開發(fā)實(shí)用小知識的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • Java-lambda表達(dá)式入門看這一篇就夠了

    Java-lambda表達(dá)式入門看這一篇就夠了

    lambda表達(dá)式最簡單的作用就是用于簡化創(chuàng)建匿名內(nèi)部類對象,Lambda表達(dá)式是一個(gè)可傳遞的代碼塊,可以在以后執(zhí)行一次或多次,下面通過本文給大家介紹Java-lambda表達(dá)式入門教程,感興趣的朋友一起看看吧
    2021-05-05
  • linux中nohup?java?-jar啟動(dòng)java項(xiàng)目的步驟

    linux中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

最新評論