簡體   English   中英

Jackson解析錯誤:org.codehaus.jackson.map.exc.UnrecognizedPropertyException異常:無法識別的字段“ Results”

[英]Jackson parsing error: exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field “Results”

有一些這樣的主題,但是我已經讀完了,仍然沒有運氣。

我有一個要反序列化來自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:無法識別的字段“ Results”(sailpoint.doet.contentmanager.ContentManagerResponse類),在[Source:java.io.StringReader@5c6648b0;上未標記為可忽略。 第1行,第13列](通過參考鏈:sailpoint.doet.contentmanager.ContentManagerResponse [“ Results”])

您可以使用PropertyNamingStrategy.UPPER_CAMEL_CASE策略來提高POJO類的可讀性。 另外,您可以使用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接口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM