一、什么是ObjectMapper?
- ObjectMapper類是Jackson庫(kù)的主要類,它提供一些功能將數(shù)據(jù)集或?qū)ο筠D(zhuǎn)換的實(shí)現(xiàn)。
- 它將使用JsonParser和JsonGenerator實(shí)例來(lái)實(shí)現(xiàn)JSON的實(shí)際讀/寫。
二、ObjectMapper怎么使用?
2.1 配置
2.1.1 普通Java項(xiàng)目(引入如下依賴即可)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-databind</ artifactId > < version >2.9.5</ version > </ dependency > <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-core</ artifactId > < version >2.9.5</ version > </ dependency > <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-annotations</ artifactId > < version >2.9.5</ version > </ dependency > |
2.1.2 Sring Boot項(xiàng)目
重要說(shuō)明:
由于Spring Boot的自動(dòng)配置JacksonAutoConfiguration中有如下圖所示的依賴引入和配置,所以不需要我們額外配置
2.2 實(shí)戰(zhàn)
User類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Data @EqualsAndHashCode (callSuper = false ) @Accessors (chain = true ) public class User implements Serializable { private static final long serialVersionUID = 1L; // 姓名 private String name; // 性別 private String sex; // 年齡 private Integer age; } |
2.2.1 Java對(duì)象、集合轉(zhuǎn)JSON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); User user = new User(); user.setName( "張三" ); user.setAge( 20 ); user.setSex( "男" ); List<User> userList = new ArrayList<>(); userList.add(user); // 對(duì)象轉(zhuǎn)換為JSON String userJsonString = objectMapper.writeValueAsString(user); // 集合轉(zhuǎn)換為JSON String userListJsonString = objectMapper.writeValueAsString(userList); } |
2.2.2 JSON轉(zhuǎn)Java對(duì)象、集合
1
2
3
4
5
|
// JOSN轉(zhuǎn)對(duì)象(java對(duì)象) User newUser = objectMapper.readValue(userJsonString, User. class ); // JOSN轉(zhuǎn)集合(集合) List<User> list = objectMapper.readValue(userListJsonString, new TypeReference<List<User>>(){}); |
2.2.3json轉(zhuǎn)JsonNode、ObjectNode
說(shuō)明:
Jackson的JsonNode和ObjectNode兩個(gè)類,前者是不可變的,一般用于讀取。后者可變,一般用于創(chuàng)建Json對(duì)象圖。
1
2
3
4
5
6
7
8
9
10
11
12
|
// json轉(zhuǎn)JsonNode JsonNode jsonNode = objectMapper.readTree(userJsonString); String sex = jsonNode.get( "sex" ).asText(); // JsonNode轉(zhuǎn)ObjectNode ObjectNode objectNode = (ObjectNode)jsonNode; // json轉(zhuǎn)JsonNode JsonNode jsonNodeList = objectMapper.readTree(userListJsonString); // JsonNode轉(zhuǎn)ObjectNode ArrayNode arrayNode = (ArrayNode)jsonNodeList; |
2.2.4 jsonNode轉(zhuǎn)對(duì)象、集合
1
2
3
4
5
6
7
|
// jsonNode轉(zhuǎn)為json字符串 String jsonNodeString = objectMapper.writeValueAsString(jsonNode); String jsonNodeListString = objectMapper.writeValueAsString(jsonNodeList); // json字符串轉(zhuǎn)對(duì)象、集合 User user1 = objectMapper.readValue(jsonNodeString, User. class ); List<User> list1 = objectMapper.readValue(jsonNodeListString, new TypeReference<List<User>>() {}); |
2.3 注意事項(xiàng)
2.3.1微服務(wù)中從其他服務(wù)獲取過(guò)來(lái)的對(duì)象,如果從Object強(qiáng)轉(zhuǎn)為自定義的類型會(huì)報(bào)錯(cuò),利用ObjectMapper轉(zhuǎn)換。
正確示例:
說(shuō)明:Schedule類、OutpOrderBill類都是類似于User類的Java對(duì)象。
1
2
3
4
5
6
7
|
// 對(duì)象 Schedule schedule = objectMapper.convertValue(callNurseCenterService.getSchedule(registRecord.getScheCode()).getData(), Schedule. class ); // 泛型為對(duì)象的集合 List<OutpOrderBill> outpOrderBillList = objectMapper.convertValue( callChargeCenterService.getOrderBillByOrderCode(orders.getOrgCode(),orders.getOrderCode()).getData(), new TypeReference<List<OutpOrderBill>>() {}); |
2.3.2 上面轉(zhuǎn)換的過(guò)程中,如果返回的字段你不是都需要,需要忽略其中的幾個(gè)字段,在自定義的類中添加標(biāo)紅注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Data @EqualsAndHashCode (callSuper = false ) @Accessors (chain = true ) @JsonIgnoreProperties (ignoreUnknown = true ) public class User implements Serializable { private static final long serialVersionUID = 1L; ////提供有這個(gè)參數(shù),但是不想獲取 // // 姓名 // private String name; // 性別 private String sex; // 年齡 private Integer age; } |
如果不想添加注解,可以使用下面兩種方式
第一種方式:
1
2
|
ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD,Visibility.ANY); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false ); |
第二種方式:
1
2
|
ObjectMapper objectMapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false ); |
2.3.3 在轉(zhuǎn)換的過(guò)程中,有可能有的屬性被設(shè)成空就不序列化等的需求,可以在類的屬性上或直接在類上加上一下注解。用在屬性上就是只針對(duì)一個(gè)屬性,用在類上就是針對(duì)類里的所有屬性。
1
2
3
4
5
6
7
8
9
|
@JsonInclude (Include.NON_NULL) @JsonInclude (Include.Include.ALWAYS) 默認(rèn) @JsonInclude (Include.NON_DEFAULT) 屬性為默認(rèn)值不序列化 @JsonInclude (Include.NON_EMPTY) 屬性為 空(“”) 或者為 NULL 都不序列化 @JsonInclude (Include.NON_NULL) 屬性為NULL 不序列化 |
參考網(wǎng)址:
到此這篇關(guān)于Java使用ObjectMapper的簡(jiǎn)單示例的文章就介紹到這了,更多相關(guān)Java使用ObjectMapper內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/wgx519/p/13688615.html