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;
"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
}
上面的JSON是JSONObject的JSONArray,我相信实际的格式是
{
"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,因此可以使用包含branded
数组的POJO类
public class ProductResponse{
@JsonProperty("branded")
private ProductInfo[] branded;
//getters and setters
}
API调用
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;
我正在尝试发布一个自定义对象的。请求体中的JSON如下所示: 处理请求的服务器端代码: 实体: 但会引发异常:
例外情况: 套接字处理程序 我相信当JSON被解析为WorkstationRequest对象时会出现异常,原因是下面的项。这是套接字处理程序: 我不知道如何开始调试它。堆栈跟踪从未触及我的应用程序。我正在使用部署我的.jar,并使用执行它
在执行从serviceA到serviceB的请求时, serviceA中的测试请求是
问题内容: 我正在尝试发布自定义对象。我在请求正文中的JSON是这样的: 处理请求的服务器端代码: 实体: 但是抛出一个异常: 问题答案: 问题是JSON-默认情况下不能反序列化为,因为它实际上不是JSON数组-看起来像这样: 由于你没有控制反序列化的确切过程(RestEasy确实如此)- 第一个选择是简单地将JSON作为a注入String,然后控制反序列化过程: 你不必自己进行操作便会失去一些便
我想用Spring Boot编写一个小而简单的REST服务。下面是REST服务代码: 我发送的JSON对象如下: 警告964---[XNIO-2 task-7].w.s.m.s.DefaultHandlerExceptionResolver:无法读取HTTP消息:org.SpringFramework.HTTP.Converter.HttpMessageNotReadableException:无
下面有一个JSON,我试图使用Jackson将其解析为POJO 我的响应POJO片段如下 以及他们的吸气手和二传手 当我得到“org.codehaus.jackson.map.jsonMappingException:Can not deserialize instance of java.util.list out of START_OBJECT token”时,我的声明是否错误? 解析逻辑如下