代码之家  ›  专栏  ›  技术社区  ›  minizibi

未调用假错误解码器

  •  0
  • minizibi  · 技术社区  · 6 年前

    配置:

    @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);
        }
    }
    

    然后stacktrace显示RetryableException的invoaction,我在任何地方都看不到日志。我做错什么了吗?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Kevin Davis    6 年前

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

    如果要在重试期间应用逻辑,则需要提供自己的 Retryer 除了你的 ErrorDecoder

        2
  •  0
  •   zmag    5 年前

    这样地:

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