Rest Template自定义异常处理
我正在使用来自外部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 = statusCode;
this.body = body;
}
public HttpStatus getStatusCode() {
return statusCode;
}
public void setStatusCode(HttpStatus statusCode) {
this.statusCode = statusCode;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
最后,这是客户端代码(省略了无关的代码):
public LoginResponse doLogin(String email, String password) { HttpEntity<?> requestEntity = new HttpEntity<Object>(crateBasicAuthHeaders(email,password));
try{
ResponseEntity<LoginResponse> responseEntity = restTemplate.exchange(myBaseURL + "/user/account/" + email, HttpMethod.GET, requestEntity, LoginResponse.class);
return responseEntity.getBody();
} catch (Exception e) {
//Custom error handler in action, here we're supposed to receive a MyCustomException
if (e instanceof MyCustomException){
MyCustomException exception = (MyCustomException) e;
logger.info("An error occurred while calling api/user/account API endpoint: " + e.getMessage());
} else {
logger.info("An error occurred while trying to parse Login Response JSON object");
}
}
return null;
}
我的应用环境:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:spring="http://camel.apache.org/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- Rest template (used in bridge communication) -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="errorHandler" ref="myCustomResponseErrorHandler"></property>
</bean>
<!-- Bridge service -->
<bean id="myBridgeService" class="a.b.c.d.service.impl.MyBridgeServiceImpl"/>
<!-- Bridge error handler -->
<bean id="myCustomResponseErrorHandler" class="a.b.c.d.service.handlers.MyCustomResponseErrorHandler"/>
</beans>
我怀疑我没有正确理解此自定义错误处理的行为。每个单独的rest模板方法都可能抛出RestClientException,该异常遵循异常层次结构,它是RuntimeException的子类,而不是IOException,它是在自定义响应错误处理程序中抛出的,即:我无法在rest模板方法中捕获我的自定义异常电话。
关于如何捕获这些异常的任何线索?Spring
RestTemplate调用带有错误的Web服务并分析状态代码是高度相关的,但是从我的角度来看,尽管它是作为解决方案提出的,但我仍然遇到相同的问题。
[1]:
回答:
您已将自定义Exception
扩展自IOException
public class MyCustomException extends IOException {
该ResponseErrorHandler#handleError()
方法是从调用RestTemplate#handleResponseError(..)
其通过调用RestTemplate#doExecute(..)
。此根调用被包装在一个try-
catch块中,该块被捕获IOException
并重新包装在a中ResourceAccessException
,即a
RestClientException
。
一种可能性是抓住RestClientException
并获得成功cause
。
另一种可能性是使您的自定义Exception
成为的子类型RuntimeException
。
以上是 Rest Template自定义异常处理 的全部内容, 来源链接: utcz.com/qa/397775.html