java 在桌面应用程序中使用 RestTemplate 使用 Rest 服务时出现的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6622287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue When Consume Rest Service with RestTemplate in Desktop App
提问by Deny Prasetyo
I have a problem when Consume Rest Service with RestTemplate in Desktop App whereas the problem doesn't appear when i use in Web app.
我在桌面应用程序中使用 RestTemplate 使用 Rest 服务时遇到问题,而当我在 Web 应用程序中使用时没有出现问题。
This Is the Debugging logs
这是调试日志
15:30:40.448 [main] DEBUG o.s.web.client.RestTemplate - Reading [java.util.List] as "application/json" using [org.springframework.http.converter.json.MappingHymansonHttpMessageConverter@98adae2]
15:30:40.452 [main] DEBUG httpclient.wire.content - << "[{"name":"Indonesia","id":1},{"name":"AlaySia","id":2},{"name":"Autraliya","id":3}]"
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.mgm.domain.Country
线程“main”中的异常 java.lang.ClassCastException:java.util.LinkedHashMap 无法转换为 com.mgm.domain.Country
And this is the Code that i use.
这是我使用的代码。
String url = "http://localhost:8080/mgm/country";
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_JSON);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(mediaTypes);
HttpEntity<Country> httpEntity = new HttpEntity<Country>(null, headers);
try {
ResponseEntity<List> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, List.class);
List<Country> countries = responseEntity.getBody();
System.out.println(countries.get(0).getName());
} catch (RestClientException exception) {
exception.printStackTrace();
}
Code above doesn't give errors when i place it in web app. I use Spring Rest MVC to Provide JSON and Consume it with RestTemplate.
当我将它放在网络应用程序中时,上面的代码不会出错。我使用 Spring Rest MVC 提供 JSON 并使用 RestTemplate 使用它。
I think there is a problem when Hymanson Convert java.util.LinkedHashMap
to Country
. it Seems that countries.get(0)
actually has LinkedHashMap
type not Country
and problem will appeared when i invoke one of Country
methode like .getName()
我认为 Hymanson 转换java.util.LinkedHashMap
为Country
. 似乎countries.get(0)
实际上没有LinkedHashMap
类型Country
,当我调用其中一个Country
方法时会出现问题.getName()
回答by stoffer
Try using an array instead:
尝试改用数组:
String url = "http://localhost:8080/mgm/country";
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_JSON);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(mediaTypes);
HttpEntity<Country> httpEntity = new HttpEntity<Country>(null, headers);
try {
ResponseEntity<Country[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Country[].class);
Country[] countries = responseEntity.getBody();
System.out.println(countries[0].getName());
} catch (RestClientException exception) {
exception.printStackTrace();
}