SpringWeb 系列教程 RestTemplate 4xx/5xx 異常信息捕獲


200104-SpringWeb 系列教程 RestTemplate 4xx/5xx 異常信息捕獲

近期使用 RestTemplate 訪問外部資源時,發現一個有意思的問題。因為權限校驗失敗,對方返回的 401 的 http code,此外返回數據中也會包含一些異常提示信息;然而在使用 RestTemplate 訪問時,卻是直接拋了如下提示 401 的異常,並不能拿到提示信息

那么 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判斷有沒有問題
@Override
public 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 的時候,就會進入異常處理邏輯

@Override
public 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,有多種寫法,下面演示兩種常見的

@RestController
public class HelloRest {
  @GetMapping("401")
  public ResponseEntity<String> _401(HttpServletResponse response) {
      ResponseEntity<String> 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

@Test
public void testCode() {
    RestTemplate restTemplate = new RestTemplate();
    HttpEntity<String> 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);
}

從上面的輸出結果也可以看出來,非標准 http code 不會拋異常(原因上面有分析),接下來看一下即便是標准的 http code 也不希望拋異常的 case

@Test
public 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<String> ans = restTemplate.getForEntity(url, String.class);
    System.out.println(ans);
}

II. 其他

0. 項目

1. 一灰灰 Blog

盡信書則不如,以上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現 bug 或者有更好的建議,歡迎批評指正,不吝感激

下面一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛

一灰灰blog


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM