問題背景:java
雲計算Pass平臺版本升級,致使引用的jackson的包直接由1.*升級爲2.* 。在版本1.*中對於字段名與實際json不符的直接忽略了,而在2.*中則會報錯。諸如此類,有較大差別,須要一一排查spring
1、配置maven依賴json
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency>
https://mvnrepository.comapp
2、測試類maven
1 package com.example.myspring.transfer; 2 3 import com.example.myspring.App; 4 import com.fasterxml.jackson.core.JsonProcessingException; 5 import com.fasterxml.jackson.databind.ObjectMapper; 6 import org.junit.Test; 7 8 import java.io.IOException; 9 import java.util.Arrays; 10 import java.util.HashMap; 11 import java.util.Map; 12 13 public class JacksonTest { 14 15 private ObjectMapper objectMapper = new ObjectMapper(); 16 17 Map map = new HashMap(); 18 19 @Test 20 public void toJsonTest1() throws JsonProcessingException { 21 // 多種類型能夠先封裝爲Map 22 map.put("alibaba", new App()); 23 map.put("tengxun", Arrays.asList("weixin", "wangzherongyao")); 24 map.put("hugh", null); 25 map.put("DOU", "DOU"); 26 System.out.println(objectMapper.writeValueAsString(map)); 27 // {"alibaba":{},"DOU":"DOU","tengxun":["weixin","wangzherongyao"],"hugh":null} 28 } 29 30 @Test 31 public void toJsonTest2() throws IOException { 32 // 新加非類Stu屬性six,且值爲null,依然能夠被識別 33 String likeStu = "{\"sto\":\"001\",\"name\":\"xx\", \"six\":null}"; 34 Map<String, Object> mock = objectMapper.readValue(likeStu, Map.class); // {"sto":"001","name":"xx","six":null} 35 System.out.println(objectMapper.writeValueAsString(mock)); 36 } 37 38 public static class Stu{ 39 40 private String sto; 41 private String name; 42 43 public Stu(String sto, String name) { 44 this.sto = sto; 45 this.name = name; 46 } 47 48 public String getSto() { 49 return sto; 50 } 51 52 public void setSto(String sto) { 53 this.sto = sto; 54 } 55 56 public String getName() { 57 return name; 58 } 59 60 public void setName(String name) { 61 this.name = name; 62 } 63 } 64 }
測試結果:測試
(1)未能復現項目中報錯,推斷與版本號有關聯this
(2)2.9.5 版本能夠解析出以上結果;可是切換到 2.7.0 或者 如下版本,又報不一樣的錯雲計算
3、總結spa
不一樣版本的jackson存在較大差別,坑較多code
【參考】https://www.jianshu.com/p/4bd355715419