未调用假装错误解码器



你好,我创建了简单的错误解码器,但它没有被调用:

配置:

@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 的调用,我在任何地方都看不到我的日志。我做错了什么吗?

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

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

尝试返回非 200 状态代码。

喜欢这个:

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

最新更新