你可以假装 ErrorDecoder
https://github.com/OpenFeign/feign/wiki/Custom-error- handling
这是一个例子
public class MyErrorDecoder implements ErrorDecoder {
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
return new MyBadRequestException();
}
return defaultErrorDecoder.decode(methodKey, response);
}
}
为了让Spring能够使用ErrorDecoder,必须将其放在ApplicationContext上:
@Bean
public MyErrorDecoder myErrorDecoder() {
return new MyErrorDecoder();
}