gpt4 book ai didi

Spring RestTemplate - 覆盖 ResponseErrorHandler

转载 作者:IT老高 更新时间:2023-10-28 13:46:06 24 4
gpt4 key购买 nike

我正在通过 RestTemplate 调用 ReST 服务并尝试覆盖 Spring 3.2 中的 ResponseErrorHandler 以处理自定义错误代码。

CustomResponseErrroHandler

public class MyResponseErrorHandler implements ResponseErrorHandler {

@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
boolean hasError = false;
int rawStatusCode = response.getRawStatusCode();
if (rawStatusCode != 200){
hasError = true;
}
return hasError;
}

@Override
public void handleError(ClientHttpResponse response) throws IOException {
//String body = IOUtils.toString(response.getBody());
throw new CustomServiceException(response.getRawStatusCode() , "custom Error");
}
}

Spring 框架调用 hasError 方法而不是 handleError,所以我不能抛出我的自定义异常。在深入研究 Spring RestTemplate 源代码后,我意识到 handleResponseError 方法中的代码导致了问题 - 它正在寻找 response .getStatusCoderesponse.getStatusText 并抛出异常(因为当 Rest 服务抛出异常时 statusCode/statusText 为空)并且它从不调用自定义实现或默认 handleError 方法在下一行。

Spring RestTemplate handleResponse 方法的源代码:

private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException {
if (logger.isWarnEnabled()) {
try {
logger.warn(method.name() + " request for \"" + url + "\" resulted in " +
response.getStatusCode() + " (" + response.getStatusText() + "); invoking error handler");
}
catch (IOException e) {
// ignore
}
}
getErrorHandler().handleError(response);
}

仅供引用,虽然服务抛出异常,但我可以从响应中读取 rawstatuscode 但不能读取状态码

如何绕过此框架代码并调用我的自定义处理程序?提前感谢您的帮助。

最佳答案

以下链接包含有关 Exception Flow for Spring ResponseErrorHandler 的有用信息.

在这里添加代码,以防博客关闭:

ErrorHandler 的代码:

public class MyResponseErrorHandler implements ResponseErrorHandler {

private static final Log logger = LogFactory.getLog(MyResponseErrorHandler.class);

@Override
public void handleError(ClientHttpResponse clienthttpresponse) throws IOException {

if (clienthttpresponse.getStatusCode() == HttpStatus.FORBIDDEN) {
logger.debug(HttpStatus.FORBIDDEN + " response. Throwing authentication exception");
throw new AuthenticationException();
}
}

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

if (clienthttpresponse.getStatusCode() != HttpStatus.OK) {
logger.debug("Status code: " + clienthttpresponse.getStatusCode());
logger.debug("Response" + clienthttpresponse.getStatusText());
logger.debug(clienthttpresponse.getBody());

if (clienthttpresponse.getStatusCode() == HttpStatus.FORBIDDEN) {
logger.debug("Call returned a error 403 forbidden resposne ");
return true;
}
}
return false;
}
}

在 RestTemplate 中使用它的代码:

RestTemplate restclient = new RestTemplate();
restclient.setErrorHandler(new MyResponseErrorHandler());
ResponseEntity<String> responseEntity = clientRestTemplate.exchange(
URI,
HttpMethod.GET,
requestEntity,
String.class);
response = responseEntity.getBody();

关于Spring RestTemplate - 覆盖 ResponseErrorHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23838752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com