【发布时间】:2019-08-09 17:00:42
【问题描述】:
有一些这样的主题,但是我已经阅读了所有内容,但仍然没有运气。
我有一个类用于反序列化来自 Web 服务的一些 JSON 响应。简而言之,我花了太多时间来研究这个问题,我希望有人能找出我的方法的错误。根据标题,我使用的是 Jackson 库。
以下类的片段:
final class ContentManagerResponse implements Serializable {
@JsonProperty("Results")
private List<OrgSearchResult> results = null;
@JsonProperty("PropertiesAndFields")
private PropertiesAndFields propertiesAndFields;
@JsonProperty("TotalResults")
private Integer totalResults;
@JsonProperty("CountStringEx")
private String countStringEx;
@JsonProperty("MinimumCount")
private Integer minimumCount;
@JsonProperty("Count")
private Integer count;
@JsonProperty("HasMoreItems")
private Boolean hasMoreItems;
@JsonProperty("SearchTitle")
private String searchTitle;
@JsonProperty("HitHighlightString")
private String hitHighlightString;
@JsonProperty("TrimType")
private String trimType;
@JsonProperty("ResponseStatus")
private ResponseStatus responseStatus;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("Results")
public List<OrgSearchResult> getResults() {
return results;
}
@JsonProperty("Results")
public void setResults(List<OrgSearchResult> results) {
this.results = results;
}
//additional getters and setters.
如前所述,Results 是似乎有错误的属性。
JSON 响应如下。
{
"Results": [
{
"TrimType": "Location",
"Uri": 1684
}
],
"PropertiesAndFields": {},
"TotalResults": 1,
"CountStringEx": "1 Location",
"MinimumCount": 1,
"Count": 0,
"HasMoreItems": false,
"SearchTitle": "Locations - type:Organization and id:24221",
"HitHighlightString": "",
"TrimType": "Location",
"ResponseStatus": {}
}
我正在使用同一个类来反序列化以下响应并且它有效:
{
"Results": [
{
"LocationIsWithin": {
"Value": true
},
"LocationSortName": {
"Value": "GW_POS_3"
},
"LocationTypeOfLocation": {
"Value": "Position",
"StringValue": "Position"
},
"LocationUserType": {
"Value": "RecordsWorker",
"StringValue": "Records Co-ordinator"
},
"TrimType": "Location",
"Uri": 64092
}
],
"PropertiesAndFields": {},
"TotalResults": 1,
"MinimumCount": 0,
"Count": 0,
"HasMoreItems": false,
"TrimType": "Location",
"ResponseStatus": {}
}
错误信息是否只是误导?除了第二个(工作)有效负载没有类中的某些字段之外,该结构是相同的。我希望这个会出错。
为了它的价值,我还在下面添加了 OrgSearchResult 类:
final class OrgSearchResult implements Serializable {
@JsonProperty("TrimType") private String trimType;
@JsonProperty("Uri") private String uri;
@JsonIgnore private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getters and setters
很多故障排除。我什至尝试使用忽略属性似乎无法让它们工作。
完全错误:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 无法识别的字段“结果”(类 sailpoint.doet.contentmanager.ContentManagerResponse),未标记为 [来源:java.io.StringReader@5c6648b0;行:1,列: 13](通过引用链: 帆点.doet.contentmanager.ContentManagerResponse["结果"])
【问题讨论】:
标签: java json jackson deserialization json-deserialization