国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

使用spring boot開發時java對象和Json對象轉換的問題

瀏覽:2日期:2022-08-15 18:55:41

將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>一、構建測試項目

開發工具為: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開發時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@AllArgsConstructorpublic class UserEntity { private String userName; private int age; private String sex; }

代碼如下(示例):

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport warningswarnings.filterwarnings(’ignore’)import sslssl._create_default_https_context = ssl._create_unverified_context2.controller類

Java對象轉換為json對象

@Controllerpublic class JsonController { @GetMapping('/json1') //思考問題,正常返回它會走視圖解析器,而json需要返回的是一個字符串 //市面上有很多的第三方jar包可以實現這個功能,jackson,只需要一個簡單的注解就可以實現了 //@ResponseBody,將服務器端返回的對象轉換為json對象響應回去 @ResponseBody public String json1() throws JsonProcessingException { //需要一個jackson的對象映射器,就是一個類,使用它可以將對象直接轉換成json字符串 ObjectMapper mapper = new ObjectMapper(); //創建對象 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; }}

優化后:

@GetMapping('/json4') @ResponseBody public String json4() throws JsonProcessingException { Date date = new Date(); return JsonUtils.getJson(date); }三、gson轉換

引入上述gson依賴

Controller類

@RestControllerpublic 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類

@RestControllerpublic 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開發時java對象和Json對象轉換的問題的文章就介紹到這了,更多相關spring boot java對象和Json對象轉換內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 国产成人18黄网站免费网站 | 欧美精品自拍 | 欧美成人爽毛片在线视频 | 黄频免费影院 | 手机看片日韩日韩国产在线看 | 依依成人综合网 | 国产亚洲女在线精品 | 午夜精品同性女女 | 免费一级在线观看 | 免费高清不卡毛片在线看 | 在线91精品亚洲网站精品成人 | 亚洲乱码国产一区网址 | 欧美成人国产一区二区 | 国内自拍2020 | 手机看片精品高清国产日韩 | 久久视精品 | 国产精品欧美一区二区三区不卡 | 99久久精品国产一区二区三区 | 狠狠色丁香婷婷综合 | 亚洲高清国产一线久久 | 波多野结衣在线中文 | 午夜国产 | 一级一级毛片免费播放 | 欧美生活片在线 | 国产私拍福利精品视频推出 | 玖玖视频精品 | 亚洲一区二区免费视频 | 欧美成人www在线观看网页 | 高清国产在线观看 | 日韩欧美一级毛片在线 | 亚洲欧美视屏 | 亚洲欧美另类自拍 | 国产精品午夜波多野结衣性色 | 色三级大全高清视频在线观看 | 波野多衣在线观 | 欧美一级xxxx俄罗斯一级 | 成人免费福利网站在线看 | 免费观看欧美性一级 | 一级做a爰片性色毛片小说 一级做a爰片性色毛片中国 | 欧美精品a毛片免费观看 | 亚洲天堂免费在线视频 |