简体   繁体   English

Jackson解析错误:org.codehaus.jackson.map.exc.UnrecognizedPropertyException异常:无法识别的字段“ Results”

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

There's a few topics like this, however I've read them all and still no luck. 有一些这样的主题,但是我已经读完了,仍然没有运气。

I have a class to which I've made to deserialize some JSON responses from a web service. 我有一个要反序列化来自Web服务的JSON响应的类。 In short, I've spent too much time looking at this and I'm hoping someone can pick out the error of my ways. 简而言之,我花了太多时间研究这个问题,希望有人能找出我的方法中的错误。 As per title, I'm using the Jackson libs. 按照标题,我正在使用Jackson库。

Snippet of the class below: 以下课程的摘录:

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.

As said, Results is the property which seems to be having the error. 如前所述, Results是似乎有错误的属性。

The JSON response is below. 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": {}
}

I'm using the same class to deserialize the following response and it works: 我正在使用同一类反序列化以下响应,并且可以正常工作:

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

Is the error message just misleading? 错误消息只是误导吗? The structure is identical aside from the second (working) payload not having some of the fields present in the class. 除了第二个(有效)有效负载外,该结构相同,但类中不包含某些字段。 I'd expect this one to error if anything. 我希望这会出错。

For what its worth I've also included the OrgSearchResult class below: 对于它的价值,我还包括以下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

A lot of troubleshooting. 很多故障排除。 I've even tried to use ignore properties can't seem to get them to work. 我什至尝试使用忽略属性似乎无法使它们正常工作。

Full error: 完整错误:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Results" (Class sailpoint.doet.contentmanager.ContentManagerResponse), not marked as ignorable at [Source: java.io.StringReader@5c6648b0; org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“ Results”(sailpoint.doet.contentmanager.ContentManagerResponse类),在[Source:java.io.StringReader@5c6648b0;上未标记为可忽略。 line: 1, column: 13] (through reference chain: sailpoint.doet.contentmanager.ContentManagerResponse["Results"]) 第1行,第13列](通过参考链:sailpoint.doet.contentmanager.ContentManagerResponse [“ Results”])

You can improve readability of POJO class by using PropertyNamingStrategy.UPPER_CAMEL_CASE strategy. 您可以使用PropertyNamingStrategy.UPPER_CAMEL_CASE策略来提高POJO类的可读性。 Also, you can use JsonAnySetter annotation to read all extra properties. 另外,您可以使用JsonAnySetter批注读取所有其他属性。 Below example shows how model could look like: 以下示例显示了模型的外观:

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
}

For first JSON payload above code prints: 对于上面的代码,第一个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='{}'}

For second JSON payload above code prints: 对于上面的代码,第二个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='{}'}

You do not need to implement Serializable interface. 您不需要实现Serializable接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决异常org.codehaus.jackson.map.exc.UnrecognizedPropertyException - How to resolve exception org.codehaus.jackson.map.exc.UnrecognizedPropertyException org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“id”(类标准),未标记为可忽略 - org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field “id” (Class Criteria), not marked as ignorable 春季启动:org.codehaus.jackson.map.exc.UnrecognizedPropertyException:WebSphere中无法识别的字段“ XX” - Spring boot : org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field “XX” in WebSphere Java Jackson org.codehaus.jackson.Z1D78DC8ED51214E518B5114E518B511.FE未识别 - Java Jackson org.codehaus.jackson.map.exc.UnrecognizedPropertyException 如何修复org.codehaus.jackson.map.exc.UnrecognizedPropertyException - How to fix org.codehaus.jackson.map.exc.UnrecognizedPropertyException Jackson 反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别 - Jackson deserialization error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 对象映射器给出异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - Object Mapper giving Exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ g” - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “g” com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常 - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “message” exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM