0

I have an REST service already working which returns the sample JSON below:

{
   "_embedded" : {
     "artist" : [ {
       "name" : "+44",
       "genre" : "Rock",
       "country" : "USA",
       "id" : 469,
       "_links" : {
         "self" : {
           "href" : <ADDRESS>
         },
         "albumList" : {
           "href" : <ADDRESS>
         }
       }
     } ]
   }
}

I'm trying to consume this resource using RestTemplate like the example below

public static void main(String args[]) {
    RestTemplate restTemplate = new RestTemplate();
    Artist[] artistList = restTemplate.getForObject("http://localhost:8080/artists/search/findByName?name=+44", Artist[].class);
    for (Artist a : artistList)
        System.out.println(a.toString());
    }
}

When the debbuger hits the getForObject line, it get this error:

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: Can not deserialize instance of br.com.lagranzotto.itunes.frontend.entity.Artist[] out of START_OBJECT token

I've searched the internet extensively for about a week with no success in finding the cause of this exception.

lagranzotto
  • 327
  • 1
  • 5
  • 16

1 Answers1

1

Looks like you're using Spring Data PagedResources in your web service, so the list of Artist data being returned is encapsulated in the _embedded property in JSON. So RestTemplate doesn't know how to deserialize that.

Check out Why does RestTemplate not bind response representation to PagedResources? and look at Oliver Gierke's response.

Community
  • 1
  • 1
Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68