我有一个JSON字符串,其格式如下:
{
"response": {
"execution_status": "ready",
"report": {
"cache_hit": true,
"created_on": "2013-07-29 08:42:42",
"fact_cache_error": null,
"fact_cache_hit": true,
"header_info": null,
"name": null,
"report_size": "5871",
"row_count": "33",
"url": "report-download?id=278641c223bc4e4d63df9e83d8fcb4e6"
},
"status": "OK"
}
}
JSON的响应部分对于一堆响应类型很常见。 JSON的报告部分仅对此响应有用。因此,我创建了一个如下所示的Response类,其中包含getter和setter(为简洁起见,此处未包括getter和setter):
@JsonRootName(value = "response")
public class Response implements Serializable {
private static final long serialVersionUID = -2597493920293381637L;
@JsonProperty(value = "error")
private String error;
@JsonProperty(value = "error_code")
private String errorCode;
@JsonProperty(value = "error_id")
private String errorId;
@JsonProperty(value = "error_description")
private String errorDescription;
@JsonProperty(value = "method")
private String method;
@JsonProperty(value = "service")
private String service;
@JsonProperty(value = "status")
private String status;
@JsonProperty(value = "execution_status")
private String executionStatus;
}
然后,我创建了一个Report类,其中带有report元素中的字段,如下所示。 ReportResponse类将从Response类扩展(同样,为简洁起见,不包括getter和setter):
public class ReportResponse extends Response {
private static final long serialVersionUID = 4950819240030644407L;
@JsonProperty(value = "cache_hit")
private Boolean cacheHit;
@JsonProperty(value = "created_on")
private Timestamp createdOn;
@JsonProperty(value = "fact_cache_error")
private String factCacheError;
@JsonProperty(value = "fact_cache_hit")
private Boolean factCacheHit;
@JsonProperty(value = "header_info")
private String headerInfo;
@JsonProperty(value = "json_request")
private String jsonRequest;
@JsonProperty(value = "name")
private String name;
@JsonProperty(value = "report_size")
private Integer reportSize;
@JsonProperty(value = "row_count")
private Integer rowCount;
@JsonProperty(value = "url")
private String url;
}
现在,当我使用ObjectMapper映射到ReportResponse对象时,出现以下错误:
String jsonString = "{\"response\": {\"execution_status\": \"ready\", \"report\": {\"cache_hit\": true, \"created_on\": \"2013-07-29 09:53:44\", \"fact_cache_error\": null, \"fact_cache_hit\": false, \"header_info\": null, \"name\": null, \"report_size\": \"5871\", \"row_count\": \"33\", \"url\": \"report-download?id=2ff62c07fc3653b68f2073e7c1aa0517\"}, \"status\": \"OK\"}}";
ObjectMapper mapper = new ObjectMapper();
ReportResponse reportResponse = mapper.readValue(jsonString, ReportResponse.class);
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "report"
我知道我可以创建一个单独的Report类,然后使用@JsonProperty批注将其嵌入到ReportResponse中。有没有一种方法可以避免这种情况,并使用注释将ReportResponse类标记为可将其映射到JSON中的“report”元素的方法?
最佳答案
还没有注释可以处理这种情况。 There is a ticket requesting this feature.
Here is a brief statement from one of the owners regarding this topic.
引用上述陈述的话:
塔图·萨洛兰塔(Tatu Saloranta):“…@JsonProperty不支持转换,因为数据绑定基于增量解析,并且无法访问完整的树表示形式。支持@JsonUnwrapped是很简单的,但是可行;因此相反(“@JsonWrapped” )从理论上讲是可行的。只需大量工作即可……”