com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化Object的实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化Object的实例相关的知识,希望对你有一定的参考价值。
我陷入了JSON与Java对象之间的映射中。我正在使用RestTemplate在春季启动时进行第三方API调用。
{
"parsed": [
{
"food": {
"foodId": "<FoodID>",
"label": "apple",
"nutrients": {
"ENERC_KCAL": 52,
"PROCNT": 0.26,
"FAT": 0.17,
"CHOCDF": 13.81,
"FIBTG": 2.4
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "<Image URL>"
}
}
]
}
我为相同的类创建了下面的类(具有getter和setter)。
@Component
@Scope("prototype")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseFoodDetailsFromAPI implements Serializable{
private static final long serialVersionUID = -1927057943962033354L;
ParsesList parsed;
}
public class ParsesList implements Serializable{
private static final long serialVersionUID = 5902036842725888370L;
List<Parse> parse;
}
public class Parse implements Serializable{
private static final long serialVersionUID = -8651880919902785186L;
private Food food;
}
public class Food implements Serializable{
private static final long serialVersionUID = -5933882633489466961L;
private String foodId;
private String label;
Nutrients NutrientsObject;
private String brand;
private String category;
private String categoryLabel;
private String foodContentsLabel;
private String image;
}
public class Nutrients implements Serializable{
private static final long serialVersionUID = -8207662942326982309L;
private float ENERC_KCAL;
private float PROCNT;
private float FAT;
private float CHOCDF;
private float FIBTG;
}
但是我在例外之下
[Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting
response for type [class com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI]
and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Cannot deserialize instance of
`com.vikas.projects.organicecommerce.redisapicall.models.ParsesList`
out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot deserialize instance of `com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` out of START_ARRAY
token at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain:
com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI["parsed"])]
with root cause
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain: com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI["parsed"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1445) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1219) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1171) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1478)
添加的JSON是完整JSON的一部分。请让我知道是否需要完整的JSON。
答案
根据共享的JSON需要更正的事物的组合
@Component
@Scope("prototype")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseFoodDetailsFromAPI implements Serializable{
private static final long serialVersionUID = -1927057943962033354L;
List<Parse> parsed;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Food implements Serializable{
private static final long serialVersionUID = -5933882633489466961L;
private String foodId;
private String label;
Nutrients nutrients;
private String brand;
private String category;
private String categoryLabel;
private String foodContentsLabel;
private String image;
}
以上是关于com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化Object的实例的主要内容,如果未能解决你的问题,请参考以下文章