色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

將java對象轉(zhuǎn)換為json對象,市面上有很多第三方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)建測試項(xiàng)目

開發(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庫。

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

當(dāng)我們不用它,而采用其他第三方j(luò)ar包時,我們可以排除掉它的依賴,可以為我們的項(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@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對象轉(zhuǎn)換為json對象

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

@GetMapping('/json3') @ResponseBody public String json3() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); //Date默認(rèn)返回時間戳,所以需要關(guān)閉它的時間戳功能 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默認(rèn)返回時間戳,所以需要關(guān)閉它的時間戳功能 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轉(zhuǎn)換

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

引入相關(guān)依賴

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

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

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 三级网站免费观看 | 精品久久久久久无码中文字幕 | 久久精品国产99国产精品亚洲 | 欧美区一区二 | 日韩日韩日韩手机看片自拍 | 亚洲在线一区二区三区 | 日韩第五页 | 婷婷丁香久久 | 亚洲免费高清 | 成人精品一区二区久久 | 亚洲成人免费观看 | 三毛片| 美女视频网站免费播放视 | 91一区二区视频 | 亚洲欧美一级久久精品 | 一区二区三区在线观看免费 | 久久亚洲在线 | 久久久久亚洲精品影视 | 一级看片免费视频囗交 | 8050网午夜一级毛片免费不卡 | 在线播放波多野结衣 | 国产一区二区三区精品久久呦 | 国产亚洲一级精品久久 | 一级aaaaa毛片免费视频 | 91久久久久久久 | 国产精品毛片久久久久久久 | 久久久久女人精品毛片 | 91av综合| 久久久久久久久性潮 | 精品午夜久久网成年网 | 91久久在线 | 国产日韩不卡免费精品视频 | 亚洲国产成人99精品激情在线 | 真人一级毛片 | 国产成人麻豆tv在线观看 | 欧美大片无尺码在线观看 | 亚欧成人毛片一区二区三区四区 | 91av综合 | 男人的天堂在线 | 在线免费观看精品 | 草久在线观看 |