如何解决异常-“无法从START_OBJECT令牌中反序列化`java.util.ArrayList`的实例”

时间:2019-11-21 06:08:50

标签: java json playframework jackson deserialization

我从API获取JSON响应,并尝试将其保存在POJO SiebelResponse中。之前它可以正常工作,但是由于我的重构更改,它突然停止了,我花了很多时间来解决这个问题,但是仍然无法解决。

SHMSite类中某个地方存在一个愚蠢的错误。我不知道在哪里。当我反序列化对POJO的JSON响应时,出现错误:

Exception occured: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.confidential.api.siebel.models.SiebelResponse["SiebelMessage"]->com.confidential.api.siebel.models.SiebelMessage["SHM Site"]->com.confidential.api.siebel.models.SHMSite["Internal Product - ISS Admin"])

注意 :我正在使用Play framework

以下是JSON响应:

{
    "ErrorContextSearchSpec": "",
    "OMErrorCode": "",
    "NumOutputObjects": "1",
    "ErrorSymbol": "",
    "OMErrorSymbol": "",
    "ErrorCode": "0x0",
    "ErrorContextIntComp": "",
    "PrimaryRowId": "M-FS3P",
    "SiebelMessage": {
        "IntObjectFormat": "Siebel Hierarchical",
        "MessageId": "141-INSP7",
        "IntObjectName": "Base SHM Site GET",
        "MessageType": "Integration Object",
        "SHM Site": {
            "Price List Id": "M-FS47",
            "Name": "Allentown",
            "Currency Code": "USD",
            "Id": "M-FS3P",
            "CRS Code": "ABECY",
            "Property Id": "M-FS3P",
            "TNT Default Category Id": "M-103RF",
            "Unit of Measure": "Sq. Ft.",
            "Internal Product - ISS Admin": [
                {
                    "Name": "Gen Standard Double",
                    "Id": "141-GV8J5C",
                    "Type": "Sleeping Room"
                },
                {
                    "Name": "Gen Standard King",
                    "Id": "141-GV8J61",
                    "Type": "Sleeping Room"
                },
                {
                    "Name": "Standard",
                    "Id": "M-FS69",
                    "Type": "Sleeping Room"
                },
                {
                    "Name": "Standard",
                    "Id": "141-GV8J81",
                    "Type": "Sleeping Room"
                }
            ]
        }
    }
}

这些是我的POJO:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "ErrorContextSearchSpec",
    "OMErrorCode",
    "NumOutputObjects",
    "ErrorSymbol",
    "OMErrorSymbol",
    "ErrorCode",
    "ErrorContextIntComp",
    "PrimaryRowId",
    "SiebelMessage"
})
public class SiebelResponse {

    @JsonProperty("ErrorContextSearchSpec")
    private String errorContextSearchSpec;
    @JsonProperty("OMErrorCode")
    private String oMErrorCode;
    @JsonProperty("NumOutputObjects")
    private String numOutputObjects;
    @JsonProperty("ErrorSymbol")
    private String errorSymbol;
    @JsonProperty("OMErrorSymbol")
    private String oMErrorSymbol;
    @JsonProperty("ErrorCode")
    private String errorCode;
    @JsonProperty("ErrorContextIntComp")
    private String errorContextIntComp;
    @JsonProperty("PrimaryRowId")
    private String primaryRowId;
    @JsonProperty("SiebelMessage")
    private SiebelMessage siebelMessage;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public SiebelResponse() {
    }

    public SiebelResponse(String errorContextSearchSpec, String oMErrorCode, String numOutputObjects, String errorSymbol, String oMErrorSymbol, String errorCode, String errorContextIntComp, String primaryRowId, SiebelMessage siebelMessage) {
        super();
        this.errorContextSearchSpec = errorContextSearchSpec;
        this.oMErrorCode = oMErrorCode;
        this.numOutputObjects = numOutputObjects;
        this.errorSymbol = errorSymbol;
        this.oMErrorSymbol = oMErrorSymbol;
        this.errorCode = errorCode;
        this.errorContextIntComp = errorContextIntComp;
        this.primaryRowId = primaryRowId;
        this.siebelMessage = siebelMessage;
    }

    @JsonProperty("ErrorContextSearchSpec")
    public String getErrorContextSearchSpec() {
        return errorContextSearchSpec;
    }

    @JsonProperty("ErrorContextSearchSpec")
    public void setErrorContextSearchSpec(String errorContextSearchSpec) {
        this.errorContextSearchSpec = errorContextSearchSpec;
    }

    @JsonProperty("OMErrorCode")
    public String getOMErrorCode() {
        return oMErrorCode;
    }

    @JsonProperty("OMErrorCode")
    public void setOMErrorCode(String oMErrorCode) {
        this.oMErrorCode = oMErrorCode;
    }

    @JsonProperty("NumOutputObjects")
    public String getNumOutputObjects() {
        return numOutputObjects;
    }

    @JsonProperty("NumOutputObjects")
    public void setNumOutputObjects(String numOutputObjects) {
        this.numOutputObjects = numOutputObjects;
    }

    @JsonProperty("ErrorSymbol")
    public String getErrorSymbol() {
        return errorSymbol;
    }

    @JsonProperty("ErrorSymbol")
    public void setErrorSymbol(String errorSymbol) {
        this.errorSymbol = errorSymbol;
    }

    @JsonProperty("OMErrorSymbol")
    public String getOMErrorSymbol() {
        return oMErrorSymbol;
    }

    @JsonProperty("OMErrorSymbol")
    public void setOMErrorSymbol(String oMErrorSymbol) {
        this.oMErrorSymbol = oMErrorSymbol;
    }

    @JsonProperty("ErrorCode")
    public String getErrorCode() {
        return errorCode;
    }

    @JsonProperty("ErrorCode")
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    @JsonProperty("ErrorContextIntComp")
    public String getErrorContextIntComp() {
        return errorContextIntComp;
    }

    @JsonProperty("ErrorContextIntComp")
    public void setErrorContextIntComp(String errorContextIntComp) {
        this.errorContextIntComp = errorContextIntComp;
    }

    @JsonProperty("PrimaryRowId")
    public String getPrimaryRowId() {
        return primaryRowId;
    }

    @JsonProperty("PrimaryRowId")
    public void setPrimaryRowId(String primaryRowId) {
        this.primaryRowId = primaryRowId;
    }

    @JsonProperty("SiebelMessage")
    public SiebelMessage getSiebelMessage() {
        return siebelMessage;
    }

    @JsonProperty("SiebelMessage")
    public void setSiebelMessage(SiebelMessage siebelMessage) {
        this.siebelMessage = siebelMessage;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

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

}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "Price List Id",
    "Name",
    "Currency Code",
    "Id",
    "CRS Code",
    "Property Id",
    "TNT Default Category Id",
    "Unit of Measure",
    "ListOfInternal Product - ISS Admin",
    "Internal Product - ISS Admin"
})
public class SHMSite {

    @JsonProperty("Price List Id")
    private String priceListId;

    @JsonProperty("Name")
    private String name;

    @JsonProperty("Currency Code")
    private String currencyCode;

    @JsonProperty("Id")
    private String id;

    @JsonProperty("CRS Code")
    private String cRSCode;

    @JsonProperty("Property Id")
    private String propertyId;

    @JsonProperty("TNT Default Category Id")
    private String tNTDefaultCategoryId;

    @JsonProperty("Unit of Measure")
    private String unitOfMeasure;

    @JsonProperty("Internal Product - ISS Admin")
    private List<InternalProductDetail> internalProductISSAdmin = new ArrayList<InternalProductDetail>();

    @JsonProperty("ListOfInternal Product - ISS Admin")
    private ListOfInternalProductISSAdmin listOfInternalProductISSAdmin;

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

    public SHMSite() {
    }

    public SHMSite(String priceListId, String name, String currencyCode, String id, String cRSCode, String propertyId,
            String tNTDefaultCategoryId, String unitOfMeasure, List<InternalProductDetail> internalProductISSAdmin,
            ListOfInternalProductISSAdmin listOfInternalProductISSAdmin) {
        super();
        this.priceListId = priceListId;
        this.name = name;
        this.currencyCode = currencyCode;
        this.id = id;
        this.cRSCode = cRSCode;
        this.propertyId = propertyId;
        this.tNTDefaultCategoryId = tNTDefaultCategoryId;
        this.unitOfMeasure = unitOfMeasure;
        this.internalProductISSAdmin = internalProductISSAdmin;
        this.listOfInternalProductISSAdmin = listOfInternalProductISSAdmin;
    }

    @JsonProperty("Price List Id")
    public String getPriceListId() {
        return priceListId;
    }

    @JsonProperty("Price List Id")
    public void setPriceListId(String priceListId) {
        this.priceListId = priceListId;
    }

    @JsonProperty("Name")
    public String getName() {
        return name;
    }

    @JsonProperty("Name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("Currency Code")
    public String getCurrencyCode() {
        return currencyCode;
    }

    @JsonProperty("Currency Code")
    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    @JsonProperty("Id")
    public String getId() {
        return id;
    }

    @JsonProperty("Id")
    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("CRS Code")
    public String getCRSCode() {
        return cRSCode;
    }

    @JsonProperty("CRS Code")
    public void setCRSCode(String cRSCode) {
        this.cRSCode = cRSCode;
    }

    @JsonProperty("Property Id")
    public String getPropertyId() {
        return propertyId;
    }

    @JsonProperty("Property Id")
    public void setPropertyId(String propertyId) {
        this.propertyId = propertyId;
    }

    @JsonProperty("TNT Default Category Id")
    public String getTNTDefaultCategoryId() {
        return tNTDefaultCategoryId;
    }

    @JsonProperty("TNT Default Category Id")
    public void setTNTDefaultCategoryId(String tNTDefaultCategoryId) {
        this.tNTDefaultCategoryId = tNTDefaultCategoryId;
    }

    @JsonProperty("Unit of Measure")
    public String getUnitOfMeasure() {
        return unitOfMeasure;
    }

    @JsonProperty("Unit of Measure")
    public void setUnitOfMeasure(String unitOfMeasure) {
        this.unitOfMeasure = unitOfMeasure;
    }

    @JsonProperty("ListOfInternal Product - ISS Admin")
    public ListOfInternalProductISSAdmin getListOfInternalProductISSAdmin() {
        return listOfInternalProductISSAdmin;
    }   

    @JsonProperty("ListOfInternal Product - ISS Admin")
    public void setListOfInternalProductISSAdmin(ListOfInternalProductISSAdmin listOfInternalProductISSAdmin) {
        this.listOfInternalProductISSAdmin = listOfInternalProductISSAdmin;
    }

    @JsonProperty("Internal Product - ISS Admin")
    public List<InternalProductDetail> getInternalProductISSAdmin() {
        return internalProductISSAdmin;
    }

    @JsonProperty("Internal Product - ISS Admin")
    public void setInternalProductISSAdmin(List<InternalProductDetail> internalProductISSAdmin) {
        this.internalProductISSAdmin = internalProductISSAdmin;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

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

}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "Name",
    "Id",
    "Type"
})
public class InternalProductDetail {

    @JsonProperty("Name")
    private String name;

    @JsonProperty("Id")
    private String id;

    @JsonProperty("Type")
    private String type;

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

    public InternalProductDetail() {
    }

    public InternalProductDetail(String name, String id, String type) {
        super();
        this.name = name;
        this.id = id;
        this.type = type;
    }

    @JsonProperty("Name")
    public String getName() {
        return name;
    }

    @JsonProperty("Name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("Id")
    public String getId() {
        return id;
    }

    @JsonProperty("Id")
    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("Type")
    public String getType() {
        return type;
    }

    @JsonProperty("Type")
    public void setType(String type) {
        this.type = type;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

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

转换代码

    public SiebelResponse transformSiebelApiResponse(final WSResponse response) {
        logger.debug("Transforming siebel api response");

        final JsonNode responseJson = response.asJson();
        final SiebelResponse siebelResponse = Json.fromJson(responseJson, SiebelResponse.class);
        return siebelResponse;
    }

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "IntObjectFormat",
    "MessageId",
    "IntObjectName",
    "MessageType",
    "ListOfBase SHM Site",
    "SHM Site"
})
public class SiebelMessage {

    @JsonProperty("IntObjectFormat")
    private String intObjectFormat;

    @JsonProperty("MessageId")
    private String messageId;

    @JsonProperty("IntObjectName")
    private String intObjectName;

    @JsonProperty("MessageType")
    private String messageType;

    @JsonProperty("ListOfBase SHM Site")
    private ListOfBaseSHMSite listOfBaseSHMSite;

    @JsonProperty("SHM Site")
    private SHMSite sHMSite;

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

    @JsonProperty("IntObjectFormat")
    public String getIntObjectFormat() {
        return intObjectFormat;
    }

    @JsonProperty("IntObjectFormat")
    public void setIntObjectFormat(String intObjectFormat) {
        this.intObjectFormat = intObjectFormat;
    }

    @JsonProperty("MessageId")
    public String getMessageId() {
        return messageId;
    }

    @JsonProperty("MessageId")
    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    @JsonProperty("IntObjectName")
    public String getIntObjectName() {
        return intObjectName;
    }

    @JsonProperty("IntObjectName")
    public void setIntObjectName(String intObjectName) {
        this.intObjectName = intObjectName;
    }

    @JsonProperty("MessageType")
    public String getMessageType() {
        return messageType;
    }

    @JsonProperty("MessageType")
    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }

    @JsonProperty("ListOfBase SHM Site")
    public ListOfBaseSHMSite getListOfBaseSHMSite() {
        return listOfBaseSHMSite;
    }

    @JsonProperty("ListOfBase SHM Site")
    public void setListOfBaseSHMSite(ListOfBaseSHMSite listOfBaseSHMSite) {
        this.listOfBaseSHMSite = listOfBaseSHMSite;
    }

    @JsonProperty("SHM Site")
    public SHMSite getSHMSite() {
        return sHMSite;
    }

    @JsonProperty("SHM Site")
    public void setSHMSite(SHMSite sHMSite) {
        this.sHMSite = sHMSite;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

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

}```

0 个答案:

没有答案