且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Json 映射异常无法从 START_ARRAY 令牌反序列化实例

更新时间:2021-11-10 22:24:57

您已将 parameters 声明为单个对象,但您将其作为 JSON 文档中多个对象的数组返回.

You have declared parameters as a single object, but you are returning it as an array of multiple objects in your JSON document.

您的模型当前将参数节点定义为 ParametersType 对象:

Your model currently defines the parameters node as a ParametersType object:

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected ParametersType parameters;

这意味着您的模型对象需要一个类似于以下内容的 JSON 文档:

This means your model object is expecting a JSON document that looks like the following:

{
    "templateId": "123",
    "parameters": {
            "parameter": [
                {
                    "key": "id",
                    "value": "1",
                    "type": "STRING_TYPE"
                },
                {
                    "key": "id2",
                    "value": "12",
                    "type": "STRING_TYPE"
                }
            ]
        },
    "documentFormat": "PDF"
}

但在您的 JSON 文档中,您将返回一个 ParametersType 对象数组.因此,您需要将模型更改为 ParametersType 对象列表:

But in your JSON document you are returning an array of ParametersType objects. So you need to change your model to be a list of ParametersType objects:

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected List<ParametersType> parameters;

事实上,您正在返回一个 ParametersType 对象数组,这就是解析器抱怨无法从 START_ARRAY 中反序列化对象的原因.它正在寻找具有单个对象的节点,但在您的 JSON 中找到了一组对象.

The fact that you are returning an array of ParametersType objects is why the parser is complaining about not being able to deserialize an object out of START_ARRAY. It was looking for a node with a single object, but found an array of objects in your JSON.