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

使用spring boot開(kāi)發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問(wèn)題

 更新時(shí)間:2021年03月02日 11:17:48   作者:BBQ__XB  
這篇文章主要介紹了使用spring boot開(kāi)發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

將java對(duì)象轉(zhuǎn)換為json對(duì)象,市面上有很多第三方j(luò)ar包,如下:

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>

一、構(gòu)建測(cè)試項(xiàng)目

開(kāi)發(fā)工具為:IDEA
后端技術(shù):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相關(guān)依賴,這是因?yàn)镾pring boot的起步依賴spring-boot-starter-web 已經(jīng)為我們傳遞依賴了Jackson JSON庫(kù)。

在這里插入圖片描述

當(dāng)我們不用它,而采用其他第三方j(luò)ar包時(shí),我們可以排除掉它的依賴,可以為我們的項(xiàng)目瘦身。

<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轉(zhuǎn)換

1.構(gòu)建User實(shí)體類

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對(duì)象轉(zhuǎn)換為json對(duì)象

@Controller
public class JsonController {

  @GetMapping("/json1")
  //思考問(wèn)題,正常返回它會(huì)走視圖解析器,而json需要返回的是一個(gè)字符串
  //市面上有很多的第三方j(luò)ar包可以實(shí)現(xiàn)這個(gè)功能,jackson,只需要一個(gè)簡(jiǎn)單的注解就可以實(shí)現(xiàn)了
  //@ResponseBody,將服務(wù)器端返回的對(duì)象轉(zhuǎn)換為json對(duì)象響應(yīng)回去
  @ResponseBody
  public String json1() throws JsonProcessingException {
    //需要一個(gè)jackson的對(duì)象映射器,就是一個(gè)類,使用它可以將對(duì)象直接轉(zhuǎn)換成json字符串
    ObjectMapper mapper = new ObjectMapper();
    //創(chuàng)建對(duì)象
    UserEntity userEntity = new UserEntity("笨笨熊", 18, "男");
    System.out.println(userEntity);
    //將java對(duì)象轉(zhuǎn)換為json字符串
    String str = mapper.writeValueAsString(userEntity);
    System.out.println(str);
    //由于使用了@ResponseBody注解,這里會(huì)將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對(duì)象轉(zhuǎn)換為json對(duì)象

@GetMapping("/json3")
  @ResponseBody
  public String json3() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    //Date默認(rèn)返回時(shí)間戳,所以需要關(guān)閉它的時(shí)間戳功能
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    //時(shí)間格式化問(wèn)題 自定義時(shí)間格式對(duì)象
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //讓mapper指定時(shí)間日期格式為simpleDateFormat
    mapper.setDateFormat(simpleDateFormat);
    //寫一個(gè)時(shí)間對(duì)象
    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默認(rèn)返回時(shí)間戳,所以需要關(guān)閉它的時(shí)間戳功能
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    //時(shí)間格式化問(wèn)題 自定義時(shí)間格式對(duì)象
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
    //讓mapper指定時(shí)間日期格式為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轉(zhuǎn)換

引入上述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轉(zhuǎn)換

引入相關(guān)依賴

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;
  }
}

到此這篇關(guān)于使用spring boot開(kāi)發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問(wèn)題的文章就介紹到這了,更多相關(guān)spring boot java對(duì)象和Json對(duì)象轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))

    Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))

    這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)的相關(guān)資料,其中包括單向鏈表、雙向鏈表及鏈表反轉(zhuǎn)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2021-06-06
  • 必須掌握的十個(gè)Lambda表達(dá)式簡(jiǎn)化代碼提高生產(chǎn)力

    必須掌握的十個(gè)Lambda表達(dá)式簡(jiǎn)化代碼提高生產(chǎn)力

    這篇文章主要為大家介紹了必須掌握的十個(gè)Lambda表達(dá)式來(lái)簡(jiǎn)化代碼提高生產(chǎn)力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Java數(shù)據(jù)導(dǎo)入功能之讀取Excel文件實(shí)例

    Java數(shù)據(jù)導(dǎo)入功能之讀取Excel文件實(shí)例

    這篇文章主要介紹了Java數(shù)據(jù)導(dǎo)入功能之讀取Excel文件實(shí)例,本文給出了jar包的下載地址以及讀取Excel文件的代碼實(shí)例,需要的朋友可以參考下
    2015-06-06
  • Spring Boot與Spring Security的跨域問(wèn)題解決方案

    Spring Boot與Spring Security的跨域問(wèn)題解決方案

    跨域問(wèn)題是指在Web開(kāi)發(fā)中,瀏覽器出于安全考慮,限制了不同域名之間的資源訪問(wèn),本文重點(diǎn)給大家介紹Spring Boot與Spring Security的跨域問(wèn)題解決方案,感興趣的朋友一起看看吧
    2023-09-09
  • SpringBoot中注冊(cè)Bean的方式總結(jié)

    SpringBoot中注冊(cè)Bean的方式總結(jié)

    這篇文章主要介紹了SpringBoot中注冊(cè)Bean的方式總結(jié),@ComponentScan + @Componet相關(guān)注解,@Bean,@Import和spring.factories這四種方式,文中代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • Spring Boot 功能整合的實(shí)現(xiàn)

    Spring Boot 功能整合的實(shí)現(xiàn)

    Spring Boot生態(tài)豐富,集成也不算困難。本文簡(jiǎn)單的介紹下功能整合的步驟,最后提供一個(gè)具體的實(shí)現(xiàn)例子,學(xué)習(xí)Spring Boot的同學(xué)可以參考下
    2021-05-05
  • Springboot初始化啟動(dòng)報(bào)錯(cuò)Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource

    Springboot初始化啟動(dòng)報(bào)錯(cuò)Error?creating?bean?with?name?'da

    這篇文章主要為大家介紹了Springboot初始化啟動(dòng)報(bào)Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • java利用正則表達(dá)式處理特殊字符的方法實(shí)例

    java利用正則表達(dá)式處理特殊字符的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于java利用正則表達(dá)式處理特殊字符的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java 網(wǎng)絡(luò)爬蟲新手入門詳解

    Java 網(wǎng)絡(luò)爬蟲新手入門詳解

    這篇文章主要介紹了Java 網(wǎng)絡(luò)爬蟲新手入門詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java構(gòu)造器方法深入理解

    Java構(gòu)造器方法深入理解

    這篇文章主要介紹了Java構(gòu)造器方法深入理解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評(píng)論