Java 假装-处理下划线异常-传播错误状态

Java 假装-处理下划线异常-传播错误状态,java,hystrix,feign,Java,Hystrix,Feign,这是一个类似的问题 我有带feign和hystrix的微服务架构。 我的问题是,当下属服务返回错误响应时(比如status404),我的FallbackFactory应该传播它,但它总是返回200 这是我的密码: 错误响应编码器: @Component public class ErrorResponseDecoder implements ErrorDecoder { private ErrorDecoder delegate = new ErrorDecoder.Default(); @

这是一个类似的问题

我有带feign和hystrix的微服务架构。 我的问题是,当下属服务返回错误响应时(比如status
404
),我的FallbackFactory应该传播它,但它总是返回
200

这是我的密码:

错误响应编码器:

@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);
}
}
Http状态:200:(
我如何使它也成为404?

首先,我不需要回退机制

很可能
ResponseEntity
会起作用,但我没有用这个解决方案尝试


有关详细设计,请参见:

首先,我不需要回退机制

很可能
ResponseEntity
会起作用,但我没有用这个解决方案尝试

详细设计见:

@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
}