赞
踩
最先调用的服务接口报错,返回统一异常处理的json,结果feign是转换的类型错误。都没有打印json内容。
各种查找资料,最后发现服务返回的状态码不是200,就不会进入到Feign
的默认ErrorDecoder
中
调整被调用服务的全局异常
添加如下代码
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
- HttpServletResponse response = servletRequestAttributes.getResponse();
- response.setStatus(500);
代用服务异常信息打印
稍微舒服点,至少吧服务返回的json打印了。
自定义openfeign异常处理
- @Slf4j
- public class CustomErrorDecoder implements ErrorDecoder {
- @Override
- public Exception decode(String s, Response response) {
- Exception exception;
- try {
- String json = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
- //自定义处理
- exception = new RuntimeException(json);
- } catch (IOException ex) {
- log.error(ex.getMessage(), ex);
- exception = new RuntimeException("解析异常");
- }
- return exception;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。