'Upgraded Spring: org.springframework.web.client.UnknownContentTypeException: Could not extract response
So I upgraded Spring from 2.1.15 to 2.2.8
I have @SpringBootTests where I make a rest template and make requests to localhost.
Full error is
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class MyResponseClass] and content type [text/html;charset=utf-8]
My RestTemplateFactory
public class RestTemplateFactory {
public static RestTemplate getRestTemplate(String baseUrl) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUrl));
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
ResponseErrorHandler responseErrorHandler =
new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
return (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR
|| httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR);
}
@Override
public void handleError(ClientHttpResponse httpResponse) throws IOException {}
};
restTemplate.setErrorHandler(responseErrorHandler);
return restTemplate;
}
public static RestTemplate getMultiTenantOAuthRestTemplate(
String baseUrl, MockKeycloakUserDetails userDetails) {
RestTemplate restTemplate = getRestTemplate(baseUrl);
ClientHttpRequestInterceptor oauthInterceptor =
(request, body, execution) -> {
HttpHeaders headers = request.getHeaders();
headers.add("Authorization", String.format("Bearer %s", getToken(baseUrl, userDetails)));
headers.
return execution.execute(request, body);
};
ClientHttpRequestInterceptor tenantInterceptor =
(request, body, execution) -> {
HttpHeaders headers = request.getHeaders();
headers.add("Tenant-ID", userDetails.getTenant());
return execution.execute(request, body);
};
restTemplate.setInterceptors(Arrays.asList(oauthInterceptor, tenantInterceptor));
return restTemplate;
}
How I use the rest template
def response = restTemplateForTenant(ACME_TENANT, acmeUser_1).getForEntity(url("ApiUrl"), MyResponseClass)
When I hit the API directly from my browser, the API seems to work fine. So it seems the configuration of my RestTemplate is a problem after upgrading?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|