升级的Spring:org.springframework.web.client.UnknownContentTypeException:无法提取响应

时间:2020-06-29 12:12:48

标签: spring-boot

所以我将Spring从2.1.15升级到了2.2.8

我有@SpringBootTests,可以在其中创建一个休息模板并向本地主机发出请求。

完全错误是

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]

我的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;
  }

我如何使用其余模板

def response = restTemplateForTenant(ACME_TENANT,acmeUser_1).getForEntity(url(“ ApiUrl”),MyResponseClass)

当我直接从浏览器中访问API时,该API似乎运行良好。看来升级后我的RestTemplate的配置有问题吗?

0 个答案:

没有答案