简体   繁体   English

Spring RestTemplate处理自定义状态代码

[英]Spring RestTemplate handle custom status code

I should call some service from my application that can return unusual http status codes, such as 230, 240 etc. By default error handler I'm getting: 我应该从我的应用程序中调用一些服务,该服务可以返回异常的http状态代码,例如230、240等。默认情况下,我得到以下错误处理程序:

Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.UnknownHttpStatusCodeException: Unknown status code [230] null] with root cause...

When I use my custom error handler I can avoid this: 当我使用自定义错误处理程序时,可以避免这种情况:

@Override
    public boolean hasError(ClientHttpResponse response) throws IOException {

        int status = response.getRawStatusCode();
        if (status >= 200 && status <= 299)
            return false;

        HttpStatus statusCode = response.getStatusCode();
        if (statusCode.is2xxSuccessful())
            return false;
        HttpStatus.Series series = statusCode.series();
        return (HttpStatus.Series.CLIENT_ERROR.equals(series)
                || HttpStatus.Series.SERVER_ERROR.equals(series));
    }

But when RestTmplate tries to retrieve it falls into the same exception in MessageBodyClientHttpResponseWrapper : 但是,当RestTmplate尝试检索它时,它会陷入MessageBodyClientHttpResponseWrapper的同一异常中:

public boolean hasMessageBody() throws IOException {
        HttpStatus responseStatus = this.getStatusCode();
        if (responseStatus.is1xxInformational() || responseStatus == HttpStatus.NO_CONTENT ||
                responseStatus == HttpStatus.NOT_MODIFIED) {
            return false;
        }
        else if (this.getHeaders().getContentLength() == 0) {
            return false;
        }
        return true;
    }

How can I get response body correctly? 如何正确获得反应身体?

最后,在Spring 5解决了问题, 对其进行了测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring RestTemplate 处理异常 - Spring RestTemplate handle exceptions Spring 启动:RestTemplate POST 调用不返回创建状态码 - Spring boot : RestTemplate POST call does not return created status code 在 Spring RestTemplate 和 ResponseEntity 中设置状态码 301 或 303 - Set status code 301 or 303 in Spring RestTemplate and ResponseEntity Spring RestTemplate - 根据http状态代码读取不同的对象类型? - Spring RestTemplate - Read different object type based on http status code? Spring 中的自定义 Http 状态代码 - Custom Http Status Code in Spring Spring RestTemplate GET 请求返回 302 状态 - Spring RestTemplate GET request returns 302 status 如何使用 Spring 集成处理 resttemplate 中的 ResourceAccessException - How to handle ResourceAccessException in resttemplate using Spring integration Java RestTemplate挂在204状态码响应上 - Java RestTemplate hangs on 204 status code response 将我的自定义 http 标头添加到 Spring RestTemplate 请求/扩展 RestTemplate - Add my custom http header to Spring RestTemplate request / extend RestTemplate Spring @RestControllerAdvice 不处理状态码 400 异常 - Spring @RestControllerAdvice doesn't handle status code 400 exceptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM