Java – JSON mapping exception cannot deserialize an instance to start_ Array token
I tried to parse my JSON request to my model I don't know what's wrong with this code The syntax of JSON is also correct and annotated for the Java model I don't know why I received an error:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of ParametersType out of START_ARRAY token (through reference chain: Document["parameters"])
Java model:
@JsonIgnoreProperties( ignoreUnkNown = true ) public class Document { @XmlElement( required = true ) @JsonProperty( "templateId" ) protected String templateId; @JsonProperty( "parameters" ) @XmlElement( required = true ) protected ParametersType parameters; @JsonProperty( "documentFormat" ) @XmlElement( required = true ) protected DocumentFormatType documentFormat;
…
@JsonIgnoreProperties( ignoreUnkNown = true ) public class ParametersType { @JsonProperty( "parameter" ) protected List<ParameterType> parameter;
…
@JsonIgnoreProperties( ignoreUnkNown = true ) public class ParameterType { @XmlElement( required = true ) @JsonProperty( "key" ) protected String key; @XmlElement( required = true ) @JsonProperty( "value" ) @XmlSchemaType( name = "anySimpleType" ) protected Object value; @JsonProperty( "type" ) @XmlElement( required = true,defaultValue = "STRING_TYPE" ) protected ParamType type;
Jason Code:
{ "templateId": "123","parameters": [ { "parameter": [ { "key": "id","value": "1","type": "STRING_TYPE" },{ "key": "id2","value": "12","type": "STRING_TYPE" } ] } ],"documentFormat": "PDF" }
Solution
You declare a parameter as a single object, but you return it as an array of multiple objects in a JSON document
Your model currently defines parameter nodes as parameterstype objects:
@JsonProperty( "parameters" ) @XmlElement( required = true ) protected ParametersType parameters;
This means that your model object expects a JSON document, as follows:
{ "templateId": "123","parameters": { "parameter": [ { "key": "id","type": "STRING_TYPE" } ] },"documentFormat": "PDF" }
But in your JSON document, you returned an array of parameterstype objects Therefore, you need to change the model to the list of parameterstype objects:
@JsonProperty( "parameters" ) @XmlElement( required = true ) protected List<ParametersType> parameters;
In fact, you return an array of parameterstype objects is why the parser complains that it cannot deserialize an object at start_ Outside array It was looking for a node with a single object, but found an array of objects in JSON