netflix Feign中的自定义错误解码器有问题。 我有一个服务A和一个服务B,服务A向服务B发送了一些东西:
@FeignClient(value = "A", url = "${B.url}", configuration = MyConfig.class)
public interface NotificationClient {
@DeleteMapping(value = "/somepath", consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Void> sendSomething(MyObject myObject);
}
MyConfig看起来像:
@Configuration
public class MyConfig {
@Bean
ErrorDecoder errorDecoder() {
return new MyErrorDecoder();
}
}
还有MyErrorDecoder:
public class MyErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
return new MyClientException(response.status(), response.reason(), response.request().url());
}
if (response.status() >= 500 && response.status() <= 599) {
return new MyServerException(response.status(), response.reason(), response.request().url());
}
return errorStatus(methodKey, response);
}
}
例如MyClientException:
@Getter
public class MyClientException extends RuntimeException {
private static final long serialVersionUID = 8021218722733395779L;
private final int httpStatusCode;
public MyClientException(int status, String reason, String url){
super(String.format("Error when sending request to: ' %s ' : status: %d %s", url, status, reason != null ? reason : ""));
this.httpStatusCode = status;
}
}
我的问题是我在服务“ B”中进行了验证,并且返回http状态代码400,但是在我的服务“ A”中,我得到了状态500和类似“将请求发送到时出错:” url_from_properties / somepath的消息':状态:400“ ..为什么?我想在服务“ A”中拥有与在服务“ B”中相同的状态代码。 我在寻找答案,但是没有什么能像我期望的那样工作。怎么了?