Could Not Read Json: Can Not Deserialize Instance of Hello.Country[] Out of Start_Object Token

Could not read JSON: Can not deserialize instance of hello.Country[] out of START_OBJECT token

You need to do the following:

public class CountryInfoResponse {

@JsonProperty("geonames")
private List<Country> countries;

//getter - setter
}

RestTemplate restTemplate = new RestTemplate();
List<Country> countries = restTemplate.getForObject("http://api.geonames.org/countryInfoJSON?username=volodiaL",CountryInfoResponse.class).getCountries();

It would be great if you could use some kind of annotation to allow you to skip levels, but it's not yet possible (see this and this)

How to fix Can not deserialize instance of Object[] out of START_OBJECT token

The above JSON is JSONArray of JSONObject, i believe actual format is

  { 
"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"
}]
}

So this should map to POJO that contains branded array, so have pojo class that consists of branded array

public class ProductResponse{

@JsonProperty("branded")
private ProductInfo[] branded;

//getters and setters
}

API call

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;

"Could not read JSON document: Can not deserialize instance of java.lang.String out of START_OBJECT token↵

Your problem is in the memberGoodsDto.type. it's declared as a String in your GoodsDto class but you try to pass it a 2 fields structure in your Json:

"goodsList": [{
"type": {
"name": "Jeep",
"index": "1"
}

http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token

There is a typo (I think) in your code, you are expecting response as Request.class, Instead you have to map it to WsResponse.class

// wrong mapping of response to Request.class
restTemplate.postForObject(uriForPost, req, Request.class);

// correct mapping
WsResponse res = restTemplate.postForObject(uriForPost, req, WsResponse.class);

Exception arises due to, contacts field which is present in request and response, but in request it is array of string and in response it is array of object (contact class), so when jackson tries to deserilize response json into Request#contacts, it finds a start object token { but it was expecting a string.

Can not deserialize instance of model.* out of START_ARRAY token\n at

Your service expects a single message. However, you send an array of messages (though it only contains a single message).

So instead of:

[
{
"message": "Hello World1",
"author": "ABC"
}
]

you should just send:

{
"message": "Hello World1",
"author": "ABC"
}


Related Topics



Leave a reply



Submit