我需要解析这个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属性的引用
答案 0 :(得分:1)
没有堆栈跟踪,我们几乎不知道错误是什么。但是,从您的问题中可以看到,document
属性没有映射到Reference
类的任何字段。
因此您可以:
将document
属性映射到字段。
在Reference
类中使用@JsonIgnoreProperties("document")
忽略document
属性。或者,您可以使用@JsonIgnoreProperties(ignoreUnknown = true)
忽略所有未知属性。
通过禁用ObjectMapper
,将DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
配置为忽略未知属性。见下文:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Reference reference = mapper.readValue(jsonDpcument, Reference.class);