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.
This Is the Debugging logs
And this is the Code that i use.Code:15:30:40.448 [main] DEBUG o.s.web.client.RestTemplate - Reading [java.util.List] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@98adae2] 15:30:40.452 [main] DEBUG httpclient.wire.content - << "5" 15:30:40.452 [main] DEBUG httpclient.wire.content - << "3" 15:30:40.452 [main] DEBUG httpclient.wire.content - << "[\r]" 15:30:40.452 [main] DEBUG httpclient.wire.content - << "[\n]" 15:30:40.452 [main] DEBUG httpclient.wire.content - << "[{"name":"Indonesia","id":1},{"name":"AlaySia","id":2},{"name":"Autraliya","id":3}]" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "[\r]" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "[\n]" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "0" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "[\r]" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "[\n]" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "[\r]" 15:30:40.460 [main] DEBUG httpclient.wire.content - << "[\n]" 15:30:40.460 [main] DEBUG httpclient.wire.header - << "[\r][\n]" Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.mgm.domain.Country 15:30:40.460 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Resorting to protocol version default close connection policy 15:30:40.460 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Should NOT close connection, using HTTP/1.1 15:30:40.460 [main] DEBUG o.a.c.httpclient.HttpConnection - Releasing connection back to connection manager. 15:30:40.460 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - Freeing connection, hostConfig=HostConfiguration[host=http://localhost:8080] 15:30:40.461 [main] DEBUG o.a.c.h.util.IdleConnectionHandler - Adding connection at: 1310113840461 15:30:40.461 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - Notifying no-one, there are no waiting threads
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.Code: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(); }
I think there is a problem when Jackson 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()


Reply With Quote
