slf4j打印未捕获异常信息_SpringWeb 系列教程 RestTemplate 4xx/5xx 异常信息捕获

b5fadb8a2680472a38f6f3c83af6493a.png

近期使用 RestTemplate 访问外部资源时,发现一个有意思的问题。因为权限校验失败,对方返回的 401 的 http code,此外返回数据中也会包含一些异常提示信息;然而在使用 RestTemplate 访问时,却是直接抛了如下提示 401 的异常,并不能拿到提示信息

5fbca34a6251a15b9ab18f5a32d12c8e.png

那么 RestTemplate 如果希望可以获取到非 200 状态码返回数据时,可以怎么操作呢?

I. 异常捕获

1. 问题分析

RestTemplate 的异常处理,是借助org.springframework.web.client.ResponseErrorHandler来做的,先看一下两个核心方法

  • 下面代码来自 spring-web.5.0.7.RELEASE 版本
public interface ResponseErrorHandler {  // 判断是否有异常boolean hasError(ClientHttpResponse response) throws IOException;  // 如果有问题,进入这个方法,处理问题void handleError(ClientHttpResponse response) throws IOException;}

简单来讲,当 RestTemplate 发出请求,获取到对方相应之后,会交给ResponseErrorHandler来判断一下,返回结果是否 ok

因此接下来将目标瞄准到 RestTemplate 默认的异常处理器: org.springframework.web.client.DefaultResponseErrorHandler

a. 判定返回结果是否 ok

从源码上看,主要是根据返回的 http code 来判断是否 ok

// 根据返回的http code判断有没有问题@Overridepublic boolean hasError(ClientHttpResponse response) throws IOException {HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());return (statusCode != null && hasError(statusCode));}// 具体的判定逻辑,简单来讲,就是返回的http code是标准的4xx, 5xx,那么就认为有问题了protected boolean hasError(HttpStatus statusCode) {return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR ||statusCode.series() == HttpStatus.Series.SERVER_ERROR);}

请注意上面的实现,自定义的某些 http code 是不会被认为是异常的,因为无法转换为对应的HttpStatus (后面实例进行说明)

b. 异常处理

当上面的 hasError 返回 ture 的时候,就会进入异常处理逻辑

@Overridepublic void handleError(ClientHttpResponse response) throws IOException {HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());if (statusCode == null) {throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(),response.getHeaders(), getResponseBody(response), getCharset(response));}handleError(response, statusCode);}protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {switch (statusCode.series()) {case CLIENT_ERROR:throw new HttpClientErrorException(statusCode, response.getStatusText(),response.getHeaders(), getResponseBody(response), getCharset(response));case SERVER_ERROR:throw new HttpServerErrorException(statusCode, response.getStatusText(),response.getHeaders(), getResponseBody(response), getCharset(response));default:throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(),response.getHeaders(), getResponseBody(response), getCharset(response));}}

从上面也可以看到,异常处理逻辑很简单,直接抛异常

2. 异常捕获

定位到生面的问题之后,再想解决问题就相对简单了,自定义一个异常处理类,不管状态码返回是啥,全都认为正常即可

RestTemplate restTemplate = new RestTemplate();restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){    @Override    protected boolean hasError(HttpStatus statusCode) {        return super.hasError(statusCode);    }    @Override    public void handleError(ClientHttpResponse response) throws IOException {    }});

3. 实测

首先写两个结果,返回的 http 状态码非 200;针对返回非 200 状态码的 case,有多种写法,下面演示两种常见的

@RestControllerpublic class HelloRest {  @GetMapping("401")  public ResponseEntity _401(HttpServletResponse response) {      ResponseEntity ans =              new ResponseEntity<>("{"code": 401, "msg": "some error!"}", HttpStatus.UNAUTHORIZED);      return ans;  }  @GetMapping("525")  public String _525(HttpServletResponse response) {      response.setStatus(525);      return "{"code": 525, "msg": "自定义错误码!"}";  }}

首先来看一下自定义的 525 和标准的 401 http code,直接通过RestTemplate访问的 case

@Testpublic void testCode() {    RestTemplate restTemplate = new RestTemplate();    HttpEntity ans = restTemplate.getForEntity("http://127.0.0.1:8080/525", String.class);    System.out.println(ans);    ans = restTemplate.getForEntity("http://127.0.0.1:8080/401", String.class);    System.out.println(ans);}
8980048bdf6ee249594c7e04d51ce85e.png

从上面的输出结果也可以看出来,非标准 http code 不会抛异常(原因上面有分析),接下来看一下即便是标准的 http code 也不希望抛异常的 case

@Testpublic void testSend() {    String url = "http://127.0.0.1:8080/401";    RestTemplate restTemplate = new RestTemplate();    restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){        @Override        protected boolean hasError(HttpStatus statusCode) {            return super.hasError(statusCode);        }        @Override        public void handleError(ClientHttpResponse response) throws IOException {        }    });    HttpEntity ans = restTemplate.getForEntity(url, String.class);    System.out.println(ans);}
a7a42d541be4dd384fec1ba7876d1e43.png

II. 其他

0. 项目

  • 工程:https://github.com/liuyueyi/spring-boot-demo[1]

1. 一灰灰 Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰 Blog 个人博客 https://blog.hhui.top[2]
  • 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top[3]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值