Это похоже на вопрос Netflix Feign - Propagate Status and Exception through MicroservicesFeign - обработка сошка Исключения - распространяющиеся статус ошибки
Я имею microservices архитектуру с симулировать и Hystrix. Моя проблема заключается в том, что при недопущении службы возвращает ответ об ошибке - скажем, статус 404
- мой FallbackFactory должен распространять его, но он всегда возвращает 200
.
Вот мой код:
ErrorResponseDecoder:
@Component
public class ErrorResponseDecoder implements ErrorDecoder {
private ErrorDecoder delegate = new ErrorDecoder.Default();
@Override
public Exception decode(String methodKey, Response response) {
HttpHeaders responseHeaders = new HttpHeaders();
for(Map.Entry<String,Collection<String>> entry : response.headers().entrySet()) {
responseHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
HttpStatus statusCode = HttpStatus.valueOf(response.status());
String statusText = response.reason();
byte[] responseBody;
try {
responseBody = IOUtils.toByteArray(response.body().asInputStream());
} catch (IOException e) {
throw new RuntimeException("Failed to process response body.", e);
}
if (response.status() >= 400 && response.status() <= 499) {
return new HttpClientErrorException(statusCode, statusText, responseHeaders, responseBody, null);
}
if (response.status() >= 500 && response.status() <= 599) {
return new HttpServerErrorException(statusCode, statusText, responseHeaders, responseBody, null);
}
return delegate.decode(methodKey, response);
}
}
Запасной:
@Component
public class ServiceFallbackFactory implements FallbackFactory<ServiceFeign> {
@Override
public ServiceFeign create(final Throwable cause) {
return new ServiceFeign() {
@Override
public Response getList(String date) {
if(cause instanceof HttpStatusCodeException) {
HttpStatusCodeException exc = (HttpStatusCodeException)cause;
return Response.status(exc.getStatusCode().value()).entity(exc.getMessage()).build();
} else {
throw new RuntimeException(cause);
}
}
}
}
Выход:
{
"statusType": "NOT_FOUND",
"entity": "404 Not Found",
"entityType": "java.lang.String",
"metadata": {},
"status": 404
}
Http-статус: 200 :( Как я могу сделать это и 404?