Jackson parsing error: exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Results"

Jackson parsing error: exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Results"

有几个像这样的主题,但是我已经全部阅读了,仍然没有运气。

我有一个 class 用来反序列化来自 Web 服务的一些 JSON 响应。简而言之,我花了太多时间看这个,我希望有人能找出我方法中的错误。根据标题,我正在使用 Jackson 库。

以下 class 的片段:

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": {}
}

我正在使用相同的 class 来反序列化以下响应并且它有效:

{
    "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": {}
}

错误消息是否只是误导?除了第二个(工作的)有效载荷没有 class 中存在的某些字段外,该结构是相同的。如果有任何错误,我希望这个错误。

为了它的价值,我还包括了下面的 OrgSearchResult class:

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: Unrecognized field "Results" (Class sailpoint.doet.contentmanager.ContentManagerResponse), not marked as ignorable at [Source: java.io.StringReader@5c6648b0; line: 1, column: 13] (through reference chain: sailpoint.doet.contentmanager.ContentManagerResponse["Results"])

您可以使用 PropertyNamingStrategy.UPPER_CAMEL_CASE 策略提高 POJO class 的可读性。此外,您可以使用 JsonAnySetter 注释来读取所有额外的属性。下面的例子展示了模型的样子:

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);

        System.out.println(mapper.readValue(jsonFile, ContentManagerResponse.class));
    }

}

class ContentManagerResponse {

    private List<OrgSearchResult> results;
    private Map<String, Object> propertiesAndFields;
    private Integer totalResults;
    private String countStringEx;
    private Integer minimumCount;
    private Integer count;
    private Boolean hasMoreItems;
    private String searchTitle;
    private String hitHighlightString;
    private String trimType;
    private Map<String, Object> responseStatus;

    // getters, setters, toString
}

class OrgSearchResult {

    private String trimType;
    private String uri;

    private Map<String, Object> additionalProperties = new HashMap<>();

    @JsonAnySetter
    public void additionalProperties(String name, Object value) {
        additionalProperties.put(name, value);
    }

    // getters, setters, toString
}

对于上面代码打印的第一个 JSON 负载:

ContentManagerResponse{results=[OrgSearchResult{trimType='Location', uri='1684', additionalProperties={}}], propertiesAndFields={}, totalResults=1, countStringEx='1 Location', minimumCount=1, count=0, hasMoreItems=false, searchTitle='Locations - type:Organization and id:24221', hitHighlightString='', trimType='Location', responseStatus='{}'}

上面代码打印的第二个 JSON 负载:

ContentManagerResponse{results=[OrgSearchResult{trimType='Location', uri='64092', additionalProperties={LocationSortName={Value=GW_POS_3}, LocationUserType={Value=RecordsWorker, StringValue=Records Co-ordinator}, LocationIsWithin={Value=true}, LocationTypeOfLocation={Value=Position, StringValue=Position}}}], propertiesAndFields={}, totalResults=1, countStringEx='null', minimumCount=0, count=0, hasMoreItems=false, searchTitle='null', hitHighlightString='null', trimType='Location', responseStatus='{}'}

您不需要实现 Serializable 接口。