相关疑难解决方法(0)

Rest模板自定义异常处理

我正在使用来自外部API的一些REST端点,我正在使用Rest Template接口来实现此目的.当我从这些调用中收到某些HTTP状态代码时,我希望能够抛出自定义应用程序异常.为了实现它,我正在实现ResponseErrorHandler接口,如下所示:

public class MyCustomResponseErrorHandler implements ResponseErrorHandler {

    private ResponseErrorHandler myErrorHandler = new DefaultResponseErrorHandler();

    public boolean hasError(ClientHttpResponse response) throws IOException {
        return myErrorHandler.hasError(response);
    }

    public void handleError(ClientHttpResponse response) throws IOException {
        String body = IOUtils.toString(response.getBody());
        MyCustomException exception = new MyCustomException(response.getStatusCode(), body, body);
        throw exception;
    }

}

public class MyCustomException extends IOException {

    private HttpStatus statusCode;

    private String body;

    public MyCustomException(String msg) {
        super(msg);
        // TODO Auto-generated constructor stub
    }

    public MyCustomException(HttpStatus statusCode, String body, String msg) {
        super(msg);
        this.statusCode = …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc

7
推荐指数
1
解决办法
3万
查看次数

标签 统计

spring ×1

spring-mvc ×1