【SpringBoot WEB 系列】RestTemplate 之非 200 状态码信息捕获
前面介绍的 RestTemplate 的使用,都是接口正常返回 200 的状态码 case,当返回非 200 状态码时,会直接抛异常,如果我希望能捕获异常,并针对正常获取返回的 message,可以如何处理呢?
<!-- more -->
博文测试项目完全基于【WEB 系列】RestTemplate 基础用法小结的项目环境,建议配合查看
基本环境:IDEA
+ maven
+ SpringBoot 2.2.1.RELEASE
测试的 REST 服务借助前一篇的鉴权,如果鉴权失败,则返回 401 状态码,具体实现如下
private String getHeaders(HttpServletRequest request) {
Enumeration<String> headerNames = request.getHeaderNames();
String name;
JSONObject headers = new JSONObject();
while (headerNames.hasMoreElements()) {
name = headerNames.nextElement();
headers.put(name, request.getHeader(name));
}
return headers.toJSONString();
}
private String getParams(HttpServletRequest request) {
return JSONObject.toJSONString(request.getParameterMap());
}
private String getCookies(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null || cookies.length == 0) {
return "";
}
JSONObject ck = new JSONObject();
for (Cookie cookie : cookies) {
ck.put(cookie.getName(), cookie.getValue());
}
return ck.toJSONString();
}
private String buildResult(HttpServletRequest request) {
return buildResult(request, null);
}
private String buildResult(HttpServletRequest request, Object obj) {
String params = getParams(request);
String headers = getHeaders(request);
String cookies = getCookies(request);
if (obj != null) {
params += " | " + obj;
}
return "params: " + params + "\nheaders: " + headers + "\ncookies: " + cookies;
}
/**
* 标准的http auth验证
*
* @param request
* @param response
* @return
* @throws IOException
*/
@GetMapping(path = "auth")
public String auth(HttpServletRequest request, HttpServletResponse response) throws IOException {
String auth = request.getHeader("Authorization");
if (StringUtils.isEmpty(auth)) {
response.setStatus(401);
response.setHeader("WWW-Authenticate", "Basic realm=\"input username and password\"");
return buildResult(request) + "\n>>>no auth header";
}
String[] userAndPass = new String(new BASE64Decoder().decodeBuffer(auth.split(" ")[1])).split(":");
if (userAndPass.length < 2) {
response.setStatus(401);
response.setHeader("WWW-Authenticate", "Basic realm=\"input username and password\"");
return buildResult(request) + "\n>>>illegal auth: " + auth;
}
if ("user".equalsIgnoreCase(userAndPass[0]) && "pwd".equalsIgnoreCase(userAndPass[1])) {
return buildResult(request) + "\n>>>auth: success!";
}
response.setStatus(401);
response.setHeader("WWW-Authenticate", "Basic realm=\"input username and password\"");
return buildResult(request) + "\n>>>illegal user or pwd!";
}
一个简单的鉴权逻辑如上,从请求头中拿到Authorization
对应的 value,并解析用户名密码,如果满足则正确返回;如果不存在 or 不满足,则返回 http 状态码为 401,并携带对应的提示信息
当我们直接像之前一样使用 RestTemplate 时,看一下效果如何
try {
// 如果返回状态码不是200,则直接抛异常,无法拿到responseBody
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> ans =
restTemplate.getForEntity("http://127.0.0.1:8080/auth?name=一灰灰&age=20", String.class);
log.info("exception with no auth res: {}", ans);
} catch (Exception e) {
log.info("exception with no auth error: {}", e);
}
输出如下,走入了 catch 逻辑,从异常堆栈中,也只能看到401 Unauthorized
,拿不到返回的 Response body
(exception with no auth error: {}
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:81)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:123)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:785)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:345)
at com.git.hui.boot.resttemplate.rest.RestTemplateDemo.exception(RestTemplateDemo.java:354)
...
更详细原理定位请参考:【WEB 系列】RestTemplate 4xx/5xx 异常信息捕获
为了处理上面的问题,我们可以设置自定义的ResponseErrorHandler
来处理
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
return false;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
log.info("some error!");
}
});
HttpEntity<String> ans = restTemplate.getForEntity("http://127.0.0.1:8080/auth?name=一灰灰&age=20", String.class);
log.info("exception with no auth after errorHandler res: {}", ans);
输出如下, 401 为返回的状态码,其中也包含了 ResponseBody,然后再业务中根据状态码和返回结果进行处理即可
(exception with no auth after errorHandler res: <401,params: {"name":["一灰灰"],"age":["20"]}
headers: {"host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Java/1.8.0_171"}
cookies:
>>>no auth header,[WWW-Authenticate:"Basic realm="input username and password"", Content-Type:"text/plain;charset=UTF-8", Content-Length:"227", Date:"Mon, 29 Jun 2020 09:57:06 GMT"]>
博文
源码
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。