杰克逊ObjectMapper:readValue返回null [英] Jackson ObjectMapper: readValue returns null
问题描述
我需要解析这个json:
I need to parse this 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
}
当前,我正在使用以下代码:
Currently, I'm using this code:
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
类的属性并不完全相同,但是我希望获得具有"common" json属性的引用
I know that json schema and Reference
class propoerties are not exactly the same, but I expected to get a reference with "common" json properties
推荐答案
如果没有堆栈跟踪,我们几乎不知道错误是什么.但是,从该问题可以看出,document
属性没有映射到Reference
类的任何字段.
Without the stack trace, we are almost clueless on what the error is. However, from what can be seen in this question, the document
property in not mapped to any field of the Reference
class.
因此您可以:
So you can either:
-
将
document
属性映射到字段.
Map the
document
property to a field.
使用 <Reference
类中的c8> 忽略document
属性.或者,您可以使用 @JsonIgnoreProperties(ignoreUnknown = true)
忽略任何未知属性.
Use @JsonIgnoreProperties("document")
in the Reference
class to ignore the document
property. Alternatively you can use @JsonIgnoreProperties(ignoreUnknown = true)
to ignore any unknown properties.
配置您的 ObjectMapper
通过禁用 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
.参见下文:
Configure your ObjectMapper
to ignore unknown properties by disabling DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
. See below:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Reference reference = mapper.readValue(jsonDocument, Reference.class);
这篇关于杰克逊ObjectMapper:readValue返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!