重写定义Spring Boot FeignClient 捕获异常信息
2021-03-04 04:28
标签:reading mat param str 捕获异常 extends orm key ace FeignClient 默认的解析器: public static FeignException errorStatus(String methodKey, Response response) { { @Configuration @Override } catch (IOException ignored) { public class BaseException extends RuntimeException { public int getStatus() { public void setStatus(int status) { public BaseException() { public BaseException(String message, int status) { public BaseException(String message) { public BaseException(String message, Throwable cause) { public BaseException(Throwable cause) { public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { { 重写定义Spring Boot FeignClient 捕获异常信息 标签:reading mat param str 捕获异常 extends orm key ace 原文地址:https://www.cnblogs.com/javalinux/p/14365203.html
// 这里做了处理
String message = format("status %s reading %s", response.status(), methodKey);
try {
if (response.body() != null) {
String body = Util.toString(response.body().asReader());
message += "; content:\n" + body;
}
} catch (IOException ignored) { // NOPMD
}
return new FeignException(response.status(), message);
}
默认截获的异常如下:
"timestamp": "2019-02-24 17:15:19",
"status": 500,
"error": "Internal Server Error",
"message": "status 400 reading PaymentInterface#methodName(ParamType,ParamType)
content: {"type":"http://httpstatus.es/404","title":"未找到资源","status":400,"detail":"这里是详细的异常信息"} ",
"path": "/oauth/token"
}
自定义解析器:
public class FeignErrorDecoder implements ErrorDecoder {
public Exception decode(String methodKey, Response response) {
try {
// 这里直接拿到我们抛出的异常信息
String message = Util.toString(response.body().asReader());
try {
JSONObject jsonObject = new JSONObject(message);
return new BaseException(jsonObject.getString("message"),jsonObject.getInt("status"));
} catch (JSONException e) {
e.printStackTrace();
}
}
return decode(methodKey, response);
}
}
其中 BaseException类如:
private int status ;
return status;
}
this.status = status;
}
}
super(message);
this.status = status;
}
super(message);
}
super(message, cause);
}
super(cause);
}
super(message, cause, enableSuppression, writableStackTrace);
}
}
异常格式:
"timestamp": "2019-02-24 17:15:19",
"status": 500,
"error": "Internal Server Error",
"message": "用户不存在",
"path": "/oauth/token"
}
转载于:https://blog.51cto.com/4925054/2354156
下一篇:c++学习草稿
文章标题:重写定义Spring Boot FeignClient 捕获异常信息
文章链接:http://soscw.com/index.php/essay/59832.html