问题
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"
}
}
to a object Reference
class:
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...
}
where Metadata
is:
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();
}
The problem is that this.mapper.readValue(...)
returns null
.
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
回答1:
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:
Map the
document
property to a field.Use @JsonIgnoreProperties("document") in the
Reference
class to ignore thedocument
property. Alternatively you can use @JsonIgnoreProperties(ignoreUnknown = true) to ignore any 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);
来源:https://stackoverflow.com/questions/52643935/jackson-objectmapper-readvalue-returns-null