杰克逊ObjectMapper:readValue返回null

时间:2018-10-04 10:00:38

标签: java jackson

我需要解析这个json:

{
   "id":"cpd4-b39c4b2a-b5cb-4581-b519-6034aaa7fe4e",
   "transactionId":"768a9be4-b5b3-452f-9bd3-9fff2e9ace5c",
   "status":"PUBLIC",
   "confidential":true,
   "expiringAt":1231231,
   "locked":true,
   "metadata":[
      {
         "user":"admin",
         "creationTimestamp":1538578453285,
         "value":"metadata"
      }
   ],
   "security":"read",
   "timestampCreation":1538578453285,
   "userCreation":"admin",
   "appCreation":"app",
   "document":{
      "id":null,
      "transactionId":"768a9be4-b5b3-452f-9bd3-9fff2e9ace5c",
      "docId":"68aab3799a9380fe82ed43ff2d46a5b07da1b270-1282",
      "size":1282,
      "name":"pom.xml",
      "alias":"alias",
      "hash":"68aab3799a9380fe82ed43ff2d46a5b07da1b270",
      "title":"title",
      "encoding":"UTF-8",
      "mimeType":"application/xml"
   }
}

到对象Reference类:

public class Reference {

    private String id;
    private String transactionId;

    private DocumentStatus status;
    private Boolean confidential;
    private Integer expiringAt;
    private Boolean locked;

    private List<Metadata> metadata;
    private String security;

    // IDReferenciaAlta
    private Date timestampCreation;
    private String userCreation;
    private String appCreation;

    private Date timestampModified;
    private String userModified;
    private String appModified;

    private Date timestampDeletion;
    private String userDeletion;
    private String appDeletion;

    //getters and setters...
}

其中Metadata是:

public class Metadata {

    private String user;
    private Date creationTimestamp;
    private String value;

    //getters an setters
}

当前,我正在使用以下代码:

Reference reference = null;
try {
    reference = this.mapper.readValue(jsonDocument, Reference.class);
} catch (IOException e1) {
    // TODO: Throw domain exception...
    e1.printStackTrace();
}

问题是this.mapper.readValue(...)返回null

我知道json模式和Reference类的属性并不完全相同,但是我希望能获得具有“常见” json属性的引用

1 个答案:

答案 0 :(得分:1)

没有堆栈跟踪,我们几乎不知道错误是什么。但是,从您的问题中可以看到,document属性没有映射到Reference类的任何字段。

因此您可以: