restTemplate 调用异常处理,处理http协议层的错误状态40*,30*等

Posted 零度anngle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了restTemplate 调用异常处理,处理http协议层的错误状态40*,30*等相关的知识,希望对你有一定的参考价值。

我们先来说说问问的所在

Rest 接口中的异常,如何能在RestTemplate 中显示出来.RestTemplate 中try catch 的 e.getMessage()只能是请求本身代码异常.(400 null 或者 500 null),但是 用postman 调用的时候是可以显示错误信息的.这是怎么回事?

问题代码

  • Rest 接口
    @PostMapping("/test")
    public ResponseEntity save(@RequestBody List<VoOrder> vos)
        System.out.println(JSON.toJSONString(vos));
//        return new ResponseEntity("fuck you ", HttpStatus.BAD_GATEWAY) ;
//        return new ResponseEntity("fuck you ", HttpStatus.OK) ; //只有ok 能走通
        return new ResponseEntity("fuck you ", HttpStatus.NOT_FOUND) ;

  • RestTemplate调用
RestTemplate restTemplate = new RestTemplate();
List<VoOrder> vos = new ArrayList<>();
try 
    ResponseEntity responseEntity = restTemplate.postForEntity("url",vos, Object.class);
catch (Exception e) 
     e.printStackTrace();
    System.out.println(e.getMessage());


说明try catch 只能获取restTemplate本事是否调取成功,不成功则异常.

##寻求解决办法

restTemplate 对错误处理有个方法,从中获取到response ,从response中获取body即可,说的容易,来看代码

  • RestTemplate调用
RestTemplate restTemplate = new RestTemplate();
List<VoOrder> vos = new ArrayList<>();
try 
        // 错误处理
       restTemplate.setErrorHandler(new CustomResponseErrorHandler());
       ResponseEntity responseEntity = restTemplate.postForEntity("/test",vos, Object.class);
catch (CustomException e) 
     e.printStackTrace();
     System.out.println(e.getBody());
      System.out.println(e.getMessage());


  • CustomResponseErrorHandler 错误处理类


public class CustomResponseErrorHandler implements ResponseErrorHandler 

  private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
   @Override
  public void handleError(ClientHttpResponse response) throws IOException 

// 队请求头的处理
    List<String> customHeader = response.getHeaders().get("x-app-err-id");

    String svcErrorMessageID = "";
    if (customHeader != null) 
      svcErrorMessageID = customHeader.get(0);
    
//对body 的处理 (inputStream)
    String body = convertStreamToString(response.getBody());

    try 

      errorHandler.handleError(response);

     catch (RestClientException scx) 

      throw new CustomException(scx.getMessage(), scx, body);
    
  
   @Override
  public boolean hasError(ClientHttpResponse response) throws IOException 
    return errorHandler.hasError(response);
  

// inputStream 装换为 string 
  private String convertStreamToString(InputStream is) 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try 
      while ((line = reader.readLine()) != null) 
        sb.append(line);
      
     catch (IOException e) 
      e.printStackTrace();
     finally 
      try 
        is.close();
       catch (IOException e) 
        e.printStackTrace();
      
    

    return sb.toString();
  


  • 自定义异常捕获的类

public class CustomException extends RestClientException 

  private RestClientException restClientException;
  private String body;

  public RestClientException getRestClientException() 
    return restClientException;
  

  public void setRestClientException(RestClientException restClientException) 
    this.restClientException = restClientException;
  

  public String getBody() 
    return body;
  

  public void setBody(String body) 
    this.body = body;
  


  public CustomException(String msg, RestClientException restClientException, String body) 
    super(msg);
    this.restClientException = restClientException;
    this.body = body;
  



大功告成

总结下

主要就是这个restTemplate 没有对错误请求做处理,只有正确请求.所以需要手动增加setErrorHandler 方法处理.

可参考:

  1. https://stackoverflow.com/questions/7878002/resttemplate-handling-response-headers-body-in-exceptions-restclientexception
  2. http://www.developerq.com/article/1504414182

以上是关于restTemplate 调用异常处理,处理http协议层的错误状态40*,30*等的主要内容,如果未能解决你的问题,请参考以下文章

Spring Resttemplate 异常处理

使用 RestTemplate 测试 ExceptionHandler

自定义 RestTemplate 异常处理 (转)

Spring RestTemplate 透传Cookie

SpringBoot 使用 RestTemplate 调用exchange方法 显示错误信息

使用RestTemplate请求,报java.lang.IllegalStateException: No instances avaliable for ip地址 异常