欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何使用springboot開發(fā)時java對象和Json對象轉換的問題-創(chuàng)新互聯

小編給大家分享一下如何使用spring boot開發(fā)時java對象和Json對象轉換的問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

玉門ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

將java對象轉換為json對象,市面上有很多第三方jar包,如下:

jackson(最常用)

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.11.2</version>
</dependency>

gson

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version>
</dependency>

fastjson

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.62</version>
</dependency>

一、構建測試項目

開發(fā)工具為:IDEA
后端技術:Spring boot ,Maven

引入依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.3</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>json</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>json</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

可以從上面看出,并未引入Jackson相關依賴,這是因為Spring boot的起步依賴spring-boot-starter-web 已經為我們傳遞依賴了Jackson JSON庫。

如何使用spring boot開發(fā)時java對象和Json對象轉換的問題

當我們不用它,而采用其他第三方jar包時,我們可以排除掉它的依賴,可以為我們的項目瘦身。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
    <exclusion>
      <artifactId>jackson-core</artifactId>
       <groupId>com.fasterxml.jackson.core</groupId>
     </exclusion>
   </exclusions>
</dependency>

二、jackson轉換

1.構建User實體類

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserEntity {


  private String userName;

  private int age;

  private String sex;
  
}

代碼如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

2.controller類

Java對象轉換為json對象

@Controller
public class JsonController {

  @GetMapping("/json1")
  //思考問題,正常返回它會走視圖解析器,而json需要返回的是一個字符串
  //市面上有很多的第三方jar包可以實現這個功能,jackson,只需要一個簡單的注解就可以實現了
  //@ResponseBody,將服務器端返回的對象轉換為json對象響應回去
  @ResponseBody
  public String json1() throws JsonProcessingException {
    //需要一個jackson的對象映射器,就是一個類,使用它可以將對象直接轉換成json字符串
    ObjectMapper mapper = new ObjectMapper();
    //創(chuàng)建對象
    UserEntity userEntity = new UserEntity("笨笨熊", 18, "男");
    System.out.println(userEntity);
    //將java對象轉換為json字符串
    String str = mapper.writeValueAsString(userEntity);
    System.out.println(str);
    //由于使用了@ResponseBody注解,這里會將str以json格式的字符串返回。
    return str;
  }

  @GetMapping("/json2")
  @ResponseBody
  public String json2() throws JsonProcessingException {

    ArrayList<UserEntity> userEntities = new ArrayList<>();

    UserEntity user1 = new UserEntity("笨笨熊", 18, "男");
    UserEntity user2 = new UserEntity("笨笨熊", 18, "男");
    UserEntity user3 = new UserEntity("笨笨熊", 18, "男");

    userEntities.add(user1);
    userEntities.add(user2);
    userEntities.add(user3);

    return new ObjectMapper().writeValueAsString(userEntities);
  }
}

Date對象轉換為json對象

@GetMapping("/json3")
  @ResponseBody
  public String json3() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    //Date默認返回時間戳,所以需要關閉它的時間戳功能
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    //時間格式化問題 自定義時間格式對象
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //讓mapper指定時間日期格式為simpleDateFormat
    mapper.setDateFormat(simpleDateFormat);
    //寫一個時間對象
    Date date = new Date();
    return mapper.writeValueAsString(date);

  }

提取工具類JsonUtils

public class JsonUtils {

  public static String getJson(Object object){
    return getJson(object,"yyyy-MM-dd HH:mm:ss");
  }
  public static String getJson(Object object,String dateFormat) {
    ObjectMapper mapper = new ObjectMapper();
    //Date默認返回時間戳,所以需要關閉它的時間戳功能
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    //時間格式化問題 自定義時間格式對象
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
    //讓mapper指定時間日期格式為simpleDateFormat
    mapper.setDateFormat(simpleDateFormat);
    try{
      return mapper.writeValueAsString(object);
    }catch (JsonProcessingException e){
      e.printStackTrace();
    }
    return null;
  }
}

優(yōu)化后:

@GetMapping("/json4")
  @ResponseBody
  public String json4() throws JsonProcessingException {
    Date date = new Date();
    return JsonUtils.getJson(date);

  }

三、gson轉換

引入上述gson依賴

Controller類

@RestController
public class gsonController {
  @GetMapping("/gson1")
  public String json1() throws JsonProcessingException {

    ArrayList<UserEntity> userEntities = new ArrayList<>();

    UserEntity user1 = new UserEntity("笨笨熊", 18, "男");
    UserEntity user2 = new UserEntity("笨笨熊", 18, "男");
    UserEntity user3 = new UserEntity("笨笨熊", 18, "男");

    userEntities.add(user1);
    userEntities.add(user2);
    userEntities.add(user3);
    
    Gson gson = new Gson();
    String str = gson.toJson(userEntities);

    return str;
  }
}

四、fastjson轉換

引入相關依賴

Controller類

@RestController
public class FastJsonController {
  @GetMapping("/fastjson1")
  public String json1() throws JsonProcessingException {

    ArrayList<UserEntity> userEntities = new ArrayList<>();

    UserEntity user1 = new UserEntity("笨笨熊", 18, "男");
    UserEntity user2 = new UserEntity("笨笨熊", 18, "男");
    UserEntity user3 = new UserEntity("笨笨熊", 18, "男");

    userEntities.add(user1);
    userEntities.add(user2);
    userEntities.add(user3);

    String str = JSON.toJSONString(userEntities);

    return str;
  }
}

以上是“如何使用spring boot開發(fā)時java對象和Json對象轉換的問題”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道!

名稱欄目:如何使用springboot開發(fā)時java對象和Json對象轉換的問題-創(chuàng)新互聯
鏈接URL:http://aaarwkj.com/article36/ccjgsg.html

成都網站建設公司_創(chuàng)新互聯,為您提供小程序開發(fā)網站設計、網站維護品牌網站設計、網站設計公司、虛擬主機

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都定制網站網頁設計
未满十八禁止在线播放| 国产精品主播自拍视频| 国产三级黄色片免费看| 九九在线免费视频蜜臀| 国产在线视频不卡一线路| 成人黄色三级免费网站| 国产一区二区欧美日本| 日韩不卡在线观看免费| 国产精品日本欧美久久久| 亚洲国产精品中文字幕一区久久| 午夜福利中文字幕在线亚洲| 成人久久精品一区二区| 日本女优邻居人妻中文字幕| 中文字幕欧美日韩人妻| 欧美另类亚洲综合久青草| 白白色最新福利视频二| 精品国产一区二区日韩91| 国产一区丝袜高跟在线| 操老熟女一区二区三区| 亚洲精品一区二区99| 亚洲一区二区午夜福利亚洲| 国产精品又大又黑又长又粗| 久久久精品国产亚洲av网黑人| 久久热视频这里有精品| 亚洲女人淫片在线观看| 亚洲欧美综合伊人看片综合| 日韩美女搞黄色的网站| 欧美看黄网站在线观看| 中文字幕av日韩在线| 日本在线人妻中文字幕| 欧美日韩在线高清一区二区| 国产高清白丝免费在线观看| 亚洲欧美韩国日本成人综合| 欧美日本一区二区三区免费| 亚洲另类视频一区二区| 日韩一区二区三级在线| 亚洲国产综合亚洲综合国产| 中文字幕欧美人妻在线| 美味人妻手机在线观看| 丝袜美腿精尽福利视频网址大全 | 一区二区三区深夜福利|