Sami
Sami

Reputation: 31

RestTemplate RestClientException Could not extract response: no suitable HttpMessageConverter found

I am getting this error when calling the RestTemplate method

GetStatusRestfulResponse response = restTemplate.getForObject(restRequest.getUrl(), GetStatusRestfulResponse.class,restRequest.getParams());

>org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class GetStatusRestfulResponse] and content type [application/json]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:550)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:511)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:248)

RestTemplate restTemplate = new RestTemplate();
    HttpClient httpClient   = HttpClientBuilder.create().setDefaultCredentialsProvider(setupAuthentication(restRequest)).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    requestFactory.setReadTimeout(restRequest.getReqTimeOut());
    restTemplate.setRequestFactory(requestFactory);
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
response = restTemplate.getForObject(restRequest.getUrl(),     GetStatusRestfulResponse.class,restRequest.getParams());       

Upvotes: 0

Views: 2149

Answers (1)

Sami
Sami

Reputation: 31

I was able to solve the issue . the culprit was the java object I was using GetStatusRestfulResponse. I took the following steps to debug the issue.

  1. Got the source codes for spring-web and jackson-databind.
  2. on debugging in the spring and jackson source code, realized that the issue was with the ObjectMapper not able to deserialize the java object.
  3. The issue was that my Java object had inner classes .

in order to fix the problem with ObjectMapper not able to deserialize the java object , I had to

  1. Add default no parameter constructors for the main java class and inner classes.

  2. Made the inner classes static.

this solved the issue :)

Upvotes: 3

Related Questions