【问题标题】:Feign ErrorDecoder not invoked未调用 Feign ErrorDecoder
【发布时间】:2019-06-12 23:03:31
【问题描述】:

您好,我创建了简单的 ErrorDecoder,但它没有被调用:

配置:

@Bean
UserClient userClient ( @Value( "${url}" ) final String url )
{
    return Feign
            .builder()
            .client( new OkHttpClient() )
            .errorDecoder( new FeignErrorDecoder() )
            .encoder( new GsonEncoder() )
            .decoder( new GsonDecoder() )
            .logger( new Slf4jLogger( UserClient.class ) )
            .logLevel( Level.FULL )
            .target( UserClient.class, url );
}

错误解码器:

@Slf4j
public class FeignErrorDecoder implements ErrorDecoder
{
    @Override
    public Exception decode ( String methodKey, Response response )
    {
        if(response.status() != 200) {
            log.error( "ERROR" );
        }
        return errorStatus(methodKey, response);
    }
}

然后堆栈跟踪显示 RetryableException 的调用,我在任何地方都看不到我的日志。我是不是做错了什么?

【问题讨论】:

    标签: java feign


    【解决方案1】:

    只有在收到响应且响应代码不是 2xx 时才会调用错误解码器。如果请求失败,则不会调用错误解码器,因为最大重试次数已用尽。在这种情况下,异常抛出给调用方法。

    如果您想在重试期间应用逻辑,除了您的 ErrorDecoder 之外,您还需要提供您自己的 Retryer

    【讨论】:

      【解决方案2】:

      尝试返回非 200 状态码。

      像这样:

      @ExceptionHandler(Exception.class)
      @ResponseBody
      public ResponseEntity<String> defaultException(Exception ex) {
          LogBack.error(ex.getMessage(),ex);
          return ResponseEntity.status(400).body("");
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-08
        • 2019-04-30
        • 2022-11-30
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        • 2017-12-14
        相关资源
        最近更新 更多