当前位置:   article > 正文

FeignClient 抛出异常时怎么拿到返回值?_feign errordecoder 200

feign errordecoder 200

第一种情况

最先调用的服务接口报错,返回统一异常处理的json,结果feign是转换的类型错误。都没有打印json内容。

 

第二种情况

各种查找资料,最后发现服务返回的状态码不是200,就不会进入到Feign的默认ErrorDecoder

调整被调用服务的全局异常

添加如下代码

  1. ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
  2. HttpServletResponse response = servletRequestAttributes.getResponse();
  3. response.setStatus(500);

 

代用服务异常信息打印

 稍微舒服点,至少吧服务返回的json打印了。

第三种情况

自定义openfeign异常处理

  1. @Slf4j
  2. public class CustomErrorDecoder implements ErrorDecoder {
  3. @Override
  4. public Exception decode(String s, Response response) {
  5. Exception exception;
  6. try {
  7. String json = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
  8. //自定义处理
  9. exception = new RuntimeException(json);
  10. } catch (IOException ex) {
  11. log.error(ex.getMessage(), ex);
  12. exception = new RuntimeException("解析异常");
  13. }
  14. return exception;
  15. }
  16. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/849890
推荐阅读
  

闽ICP备14008679号