为什么handleresponse(uri url,httpmethod方法,clienthttpresponse响应)resttemplate的方法。
我正在使用 RESTTEMPLATE 从我的代码中调用外部API 如下:
try {
responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
UploadResonse.class);
} catch (BusinessException ex) {
fetchErrorResponseEntity = ex.getResponseEntity();
if (fetchErrorResponseEntity.getStatusCodeValue() == 404) {
throw new BusinessException(ex.getMessage(), ErrorResponse.NOT_FOUND);
} else if (fetchErrorResponseEntity.getStatusCodeValue() == 500) {
throw new BusinessException(ex.getMessage(),
ErrorResponse.INTERNAL_SERVER_ERROR);
} else if (fetchErrorResponseEntity.getStatusCodeValue() == 400) {
throw new BusinessException(ex.getMessage(), ErrorResponse.INVALID_REQUEST);
}
}
此API呼叫返回 200 Success ,但是当我调试它时,它仍然会转到 handleresponse( URI URL,httpmethod方法,clienthttpresponse响应) restTemplate.class.class 的方法
,然后它来了我的restTemplateErrorhandler.java文件,
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse)
throws IOException {
return clientHttpResponse.getStatusCode() != HttpStatus.OK;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse)
throws IOException {
String errMessage = getErrMessage(clientHttpResponse);
HttpStatus status = clientHttpResponse.getStatusCode();
switch (status) {
case BAD_REQUEST: // 400
throw new BusinessException(errMessage,
ErrorResponse.INVALID_REQUEST);
case NOT_FOUND:
throw new BusinessException(errMessage, ErrorResponse.NOT_FOUND);
case SERVICE_UNAVAILABLE: // 503
throw new BusinessException(errMessage, ErrorResponse.TIME_OUT);
case METHOD_NOT_ALLOWED: // 405
case INTERNAL_SERVER_ERROR: // 500
default:
throw new BusinessException(errMessage,
ErrorResponse.INTERNAL_SERVER_ERROR);
}
}
有人可以帮助我租赁是否有正确的行为。 我怀疑,如果响应是 200成功,它不应转到 restTemlate.class 和 restTemplateTemplateErrorhandler.Class 当API返回 201创建状态时,此行为正在造成问题,该时间到了 handle error()方法并返回默认情况 internal_server_error 有人可以在这里帮我吗
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下代码每次响应不是 200 ok ,如果成功 201创建,则会调用错误处理程序。
尝试将实现更改为以下内容:
这更适合您的需求,因为它将将所有 2xx 状态视为成功的请求,而不是仅 200 OK ok 。
The following code will call the error handler every time the response is not 200 OK, event if it is successful like 201 Created.
Try changing the implementation to the following:
This is better suited for your needs as it will consider all 2xx status as successful requests instead of only 200 OK.
根据 ResponseerRorhandler (如果需要),该接口是RESTTEMPLATE使用的接口来确定特定响应是否有错误。
RESTTEMPLATEERHANDLER
class实施ResponseerRorhandler
。如果
haserror()
方法返回true
,那么Spring将自动调用handleRor()
方法。这是流。如果您检查
handleresponse
方法的实现,以下给出,您会发现有一个调用HaserRor
方法来检查响应是否有任何错误。Haserror
方法的默认实现将返回true是响应代码是4xx
或5xx
。如果没有错误,则方法将继续执行,并且不会调用handleror方法,如我上面所述。在代码中,您发布的
HaserRor
将返回 true 对于所有不同的200
的响应代码。这就是为什么调用handleror
。According to documentation method
handleResponse()
as it name suggests will handle the given response, perform appropriate logging and invoke theResponseErrorHandler
(if needed) which is interface used by the RestTemplate to determine whether a particular response has an error or not.RestTemplateErrorHandler
class implements implementsResponseErrorHandler
.If the
hasError()
method returnstrue
then Spring will automatically call thehandleError()
method. This is the flow.If you check implementation for
handleResponse
method, given below, you will see that there is a call tohasError
method to check if the response has any errors. Default implementation ofhasError
method will return true is response code is4XX
or5XX
. If there is no errors, method will proceed execution and handleError method won't be invoked, as I explained above.In code you posted
hasError
will return true for all response codes that are different from200
. That's whyhandleError
is invoked.