我正在尝试从RestTemplate中检索对象,但每次JsonMappingException都会发生。那是我试图将 json 对象映射到的类吗?看起来开头的数组可能有问题,但我无法弄清楚。
Caused by: com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of com.lubaszak.bean.ProductInfo[] out
of START_OBJECT token
at [Source: java.io.PushbackInputStream@10a3c667; line: 1, column: 1]
public ResponseEntity<ProductInfo[]> getProductByQuery(@PathVariable String query) {
HttpEntity<?> httpEntity = headersProvider.getHeaders();
ResponseEntity<ProductInfo[]> product = restConfig.createRestTemplate()
.exchange("https://trackapi.nutritionix.com/v2/search/instant?query={query}&common=false&branded=false",HttpMethod.GET, httpEntity, ProductInfo[].class, query);
return product;
Json 对象:
"branded": [
{
"food_name": "Big Mac",
"serving_unit": "burger",
"nix_brand_id": "513fbc1283aa2dc80c000053",
"brand_name_item_name": "McDonald's Big Mac",
"serving_qty": 1,
"nf_calories": 540,
"photo": {
"thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
"highres": null,
"is_user_uploaded": false
},
"brand_name": "McDonald's",
"region": 1,
"brand_type": 1,
"nix_item_id": "513fc9e73fe3ffd40300109f",
"locale": "en_US"
},
爪哇类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductInfo {
@JsonProperty("food_name")
public String foodName;
@JsonProperty("serving_unit")
public String servingUnit;
@JsonProperty("nix_brand_id")
public String nixBrandId;
@JsonProperty("brand_name_item_name")
public String brandNameItemName;
@JsonProperty("serving_qty")
public Integer servingQty;
@JsonProperty("nf_calories")
public Integer nfCalories;
@JsonProperty("brand_name")
public String brandName;
@JsonProperty("brand_type")
public Integer brandType;
@JsonProperty("nix_item_id")
public String nixItemId;
//getter methods
}
上面的JSONArray是JSONObject的,我相信实际格式是
{
"branded": [
{
"food_name": "Big Mac",
"serving_unit": "burger",
"nix_brand_id": "513fbc1283aa2dc80c000053",
"brand_name_item_name": "McDonald's Big Mac",
"serving_qty": 1,
"nf_calories": 540,
"photo": {
"thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
"highres": null,
"is_user_uploaded": false
},
"brand_name": "McDonald's",
"region": 1,
"brand_type": 1,
"nix_item_id": "513fc9e73fe3ffd40300109f",
"locale": "en_US"
},
{
"food_name": "Big Mac",
"serving_unit": "burger",
"nix_brand_id": "513fbc1283aa2dc80c000053",
"brand_name_item_name": "McDonald's Big Mac",
"serving_qty": 1,
"nf_calories": 540,
"photo": {
"thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
"highres": null,
"is_user_uploaded": false
},
"brand_name": "McDonald's",
"region": 1,
"brand_type": 1,
"nix_item_id": "513fc9e73fe3ffd40300109f",
"locale": "en_US"
}]
}
所以这应该映射到包含数组branded
POJO,所以有由数组组成的 pojo 类branded
public class ProductResponse{
@JsonProperty("branded")
private ProductInfo[] branded;
//getters and setters
}
接口调用
ResponseEntity<ProductResponse> product = restConfig.createRestTemplate()
.exchange("https://trackapi.nutritionix.com/v2/search/instant?query={query}&common=false&branded=false",HttpMethod.GET, httpEntity, ProductResponse.class, query);
return product;