1

hello everyone I have a problem that I do not understand why this is happening. I am working with spring boot and deploying with Kubernetes. after a while, the restTemplate stops working and I do not know what can be the cause. this is how I am creating the restTemplate class

    @Bean
    public RestTemplate vanillaRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(new RestTemplateErrorHandler());
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        return restTemplate;
    }

after a while some of my APIs that is using restTemplate returns an error like this:

{
"status": 500,
"body": "{ message: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [application/json;charset=UTF-8] , cause: null }",
}

this is only an example. resttemplate will have a problem with many classes such as Map, String, etc when this happens. and this is how I am sending the request:

ResponseEntity<List> adsRes = restTemplate.getForEntity(url, List.class);

the weird part is when I restart the app on the server everything starts working again and the APIs have no problem at all. even the ones that returned an error. I have read this question but, as you can see I have not set any interceptor or anything like that to resttemplate thanks in advance

H.Khadem
  • 50
  • 9
  • could you show how is calling to the endpoint? – tino89 Sep 02 '20 at 04:27
  • @tino89 I just added it to the question – H.Khadem Sep 02 '20 at 04:36
  • Do you have a sample of the response. It is likely that you are not getting a list back in the response. – shinjw Sep 02 '20 at 04:38
  • @shinjw well the problem is I do not have the response, but the response is a list. – H.Khadem Sep 02 '20 at 04:40
  • if the response is a list/map of values , then we create a class with a parameter of type list /map and use that class in resttemplate, something like below ResponseEntity response = restTemplate.exchange(uri, HttpMethod.GET, entity,SourceResponse.class); public class SourceResponse { private List data; private List> value; } – Vignesh_A Sep 02 '20 at 05:31

1 Answers1

0

Try replace the way how you are calling to the endpoint by the following code

ResponseEntity<YourObject[]> response = restTemplate.getForEntity("urlService",
  YourObject[].class);

YourObject[] yourObjects= response.getBody();
List<YourObject> list = Arrays.asList(yourObjects);
tino89
  • 142
  • 5
  • thank you for your answer but the problems is that when this happens it's not just List that it runs into a problem, restTemplate will have a problem with many classes such as Map, List, String, etc. also this API will work after I restart the server. – H.Khadem Sep 02 '20 at 04:50
  • add into the bean you are creating "vanillaRestTemplate": MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL)); restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter); – tino89 Sep 02 '20 at 04:59