在jackson序列化對象時,因爲其在源碼中增長了一個可見性判斷,會默認只序列化public修飾的字段和方法,形成一個可能存在的問題是你字段明明是CId,可是自動生成的get方法是getCId的話,jackson會將字段序列化成cid,形成一個反序列失敗的問題(2.0.2到最新2.9.6版本測試),附上代碼:json
public class Test { public static void main(String[] args) throws IOException { Student stu = new Student(); stu.setCId("王者"); stu.setAge(12); ObjectMapper map = new ObjectMapper(); //map.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); //map.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); String json = map.writeValueAsString(stu); System.out.println(json); } } class Student implements Serializable { private static final long serialVersionUID = 1L; private String CId; private int age; public String getCId() { return CId; } public void setCId(String CId) { this.CId = CId; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
附上運行結果:app
解決辦法:jackson換成fastjson,或者將我上面代碼中貼出來的註釋去掉就好(所有字段可見,不對get方法進行序列化)測試
有興趣的能夠研究下其源碼!this