[英]IOException: Internal Server Error when trying to consume rest service
[英]Issues when trying to consume Spring REST
我有一個REST服務已經在工作,它會返回以下示例JSON:
{
"_embedded" : {
"artist" : [ {
"name" : "+44",
"genre" : "Rock",
"country" : "USA",
"id" : 469,
"_links" : {
"self" : {
"href" : <ADDRESS>
},
"albumList" : {
"href" : <ADDRESS>
}
}
} ]
}
}
我正在嘗試使用RestTemplate消耗此資源,如下例所示
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());
}
}
當調試器擊中getForObject行時,它得到以下錯誤:
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
我已經在互聯網上進行了大約一周的廣泛搜索,但未能成功找到此異常的原因。
看起來您在Web服務中正在使用Spring Data PagedResources,因此要返回的Artist數據列表封裝在JSON的_embedded
屬性中。 所以RestTemplate不知道如何反序列化。
查看為什么RestTemplate不將響應表示綁定到PagedResources? 再看看奧利弗·吉爾克(Oliver Gierke)的回應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.