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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:40:28  来源:igfitidea点击:

Issue When Consume Rest Service with RestTemplate in Desktop App

javaspringspring-mvcresttemplate

提问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.LinkedHashMapto Country. it Seems that countries.get(0)actually has LinkedHashMaptype not Countryand problem will appeared when i invoke one of Countrymethode like .getName()

我认为 Hymanson 转换java.util.LinkedHashMapCountry. 似乎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();
}