1

I am calling a rest endpoint in my code and the endpoint is returning a simple string "true" or "false". I have upgraded my spring boot to 2.4. The below code is now throwing an exception now.

 ResponseEntity<Boolean> status = restTemplate.getForEntity(uri, Boolean.class);

org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Boolean] and content type [application/json] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126) ~[spring-web-5.3.8.jar:5.3.8] at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1037) ~[spring-web-5.3.8.jar:5.3.8] at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1020) ~[spring-web-5.3.8.jar:5.3.8] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:778) ~[spring-web-5.3.8.jar:5.3.8]

How to resolve this issue?

Regards, David

2 Answers2

0

This error happens when the restTemplate does not have suitable MessageConverters. By default restTemplate of Spring boot provides the message converters which can convert the Boolean response.

But, if you override the list of message converters for example like below - restTemplate.setMessageConverters(Arrays.asList(new ByteArrayHttpMessageConverter()));

Then, Spring boot will not be able to convert the message.

Just verify what are the message converters added to restTemplate instance that you are using and check whether MappingJackson2HttpMessageConverter is present or not. Or you can use your own custom message converter.

Vivek Vardhan
  • 1,118
  • 3
  • 21
  • 44
0

Remove the response entity things just add as below.

Boolean  status = restTemplate.getForEntity(uri, Boolean.class);
S. Anushan
  • 728
  • 1
  • 6
  • 12