How do I retrieve HTTP status code and response body when an RestClientException is thrown?

左心房为你撑大大i 提交于 2019-12-18 04:29:20

问题


The methods of RestTemplate such as postForEntity() throw RestClientException. I would like to extract the HTTP status code and response body from that exception object in the catch block. How can I do that?


回答1:


Instead of catching RestClientException, catch the special HttpClientErrorException.

Here's an example:

try {
    Link dataCenterLink = serviceInstance.getLink("dataCenter");
    String dataCenterUrl = dataCenterLink.getHref();
    DataCenterResource dataCenter =
        restTemplate.getForObject(dataCenterUrl, DataCenterResource.class);
    serviceInstance.setDataCenter(dataCenter);
} catch (HttpClientErrorException e) {
    HttpStatus status = e.getStatusCode();
    if (status != HttpStatus.NOT_FOUND) { throw e; }
}

HttpClientErrorException provides getStatusCode and getResponseBodyAsByteArray to get the status code and body, respectively.




回答2:


Catch RestClientResponseException instead. It's more generic.

From the docs: Common base class for exceptions that contain actual HTTP response data.




回答3:


In some cases, HttpClientErrorException is not thrown. For example the following method restTemplate.exchange call:

ResponseEntity<Employee[]> employees =  restTemplate.exchange(url, HttpMethod.GET, entity, Employee[].class);

Gets the http body and marshalls it to an Entity. If remote resource returns a rare error, internal marshall does not work and just a RestClientException is thrown.

restTemplate.setErrorHandler

In this case or if you want to handle any error in restTemplate operations, you could use setErrorHandler. This method receives a basic ResponseErrorHandler with helpful methods.

This method hasError allowed me to get the remote http body text and helped me to detect the error of the invocation or in the remote http remote resource:

restTemplate.setErrorHandler(new ResponseErrorHandler() {

  @Override
  public boolean hasError(ClientHttpResponse arg0) throws IOException {

    System.out.println("StatusCode from remote http resource:"+arg0.getStatusCode());
    System.out.println("RawStatusCode from remote http resource:"+arg0.getRawStatusCode());
    System.out.println("StatusText from remote http resource:"+arg0.getStatusText());

    String body = new BufferedReader(new InputStreamReader(arg0.getBody()))
          .lines().collect(Collectors.joining("\n"));

    System.out.println("Error body from remote http resource:"+body);
    return false;
  }

  @Override
  public void handleError(ClientHttpResponse arg0) throws IOException {
    // do something
  }
});

Also, you can manually evaluate the body or status and return true or false in order to flag as error or not.



来源:https://stackoverflow.com/questions/12553570/how-do-i-retrieve-http-status-code-and-response-body-when-an-restclientexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!