问题
Hello I created simple ErrorDecoder but it's not invoked:
The configuration:
@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 );
}
ErrorDecoder:
@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);
}
}
Then the stacktrace shows invoaction of RetryableException and I don't see my log anywhere. Am i doing something wrong?
回答1:
Error Decoders are invoked only when a response is received and the response code is not 2xx. Error Decoders are not invoked if the request fails because the maximum number of retries is exhausted. In that case, the Exception thrown out to the calling method.
If you want to apply logic during a retry, you will need to supply your own Retryer
in addition to your ErrorDecoder
回答2:
Attempt to return a non-200 status code.
Like this:
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<String> defaultException(Exception ex) {
LogBack.error(ex.getMessage(),ex);
return ResponseEntity.status(400).body("");
}
来源:https://stackoverflow.com/questions/54258079/feign-errordecoder-not-invoked