vai*_*hav 88 rest spring exception-handling resttemplate
以下是代码段; 基本上,当错误代码不是200时,我试图传播异常.
ResponseEntity<Object> response = restTemplate.exchange(url.toString().replace("{version}", version),
HttpMethod.POST, entity, Object.class);
if(response.getStatusCode().value()!= 200){
logger.debug("Encountered Error while Calling API");
throw new ApplicationException();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果来自服务器的500响应,我将获得异常
org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
Run Code Online (Sandbox Code Playgroud)
我真的需要在try中包装其余的模板交换方法吗?那么代码的目的是什么?
car*_*ret 103
您希望创建一个实现的类,ResponseErrorHandler然后使用它的实例来设置其余模板的错误处理:
public class MyErrorHandler implements ResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// your error handling here
}
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
...
}
}
[...]
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new MyErrorHandler());
}
Run Code Online (Sandbox Code Playgroud)
另外,Spring有类DefaultResponseErrorHandler,你可以扩展而不是实现接口,以防你只想覆盖handleError方法.
public class MyErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// your error handling here
}
}
Run Code Online (Sandbox Code Playgroud)
看看它的源代码,了解Spring如何处理HTTP错误.
mek*_*azu 34
你应该抓住一个HttpStatusCodeException例外:
try {
restTemplate.exchange(...);
} catch (HttpStatusCodeException exception) {
int statusCode = exception.getStatusCode().value();
...
}
Run Code Online (Sandbox Code Playgroud)
aus*_*rlo 29
Spring巧妙地将http错误代码视为异常,并假设您的异常处理代码具有处理错误的上下文.要使交换功能正常运行,请执行以下操作:
try {
return restTemplate.exchange(url, httpMethod, httpEntity, String.class);
} catch(HttpStatusCodeException e) {
return ResponseEntity.status(e.getRawStatusCode()).headers(e.getResponseHeaders())
.body(e.getResponseBodyAsString());
}
Run Code Online (Sandbox Code Playgroud)
这将返回响应的所有预期结果.
Inv*_*tor 18
另一种解决方案是本文结尾处“ enlian”描述的解决方案:http ://springinpractice.com/2013/10/07/handling-json-error-object-responses-with-springs-resttemplate
try{
restTemplate.exchange(...)
} catch(HttpStatusCodeException e){
String errorpayload = e.getResponseBodyAsString();
//do whatever you want
} catch(RestClientException e){
//no response payload, tell the user sth else
}
Run Code Online (Sandbox Code Playgroud)
Sia*_*ter 11
这些或其他教程都对我没有帮助。RestTemplate简直就是废话。我在写这篇文章是为了让其他人不要在这个可怕的事情上浪费时间。我调用restTemplate.exchange并获取400状态代码。该行还会引发异常,但这不是HttpStatusCodeException,而是ResourceAccessException。我试过交换对象和字符串。我试过包括ResponseErrorHandler,这是完全没有用的。
这也不是我第一次也无法使用RestTemplate做我想做的事情,而且我甚至很长时间都没有使用它。不要浪费时间。
例如,您可以使用:
Apache HttpClient:https : //mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
或OkHttp3:https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
Spring从非常大的HTTP状态代码列表中抽象出您。那就是例外的想法。看一下org.springframework.web.client.RestClientException层次结构:
在处理http响应时,有一堆类可以映射最常见的情况。http代码列表确实很大,您不需要编写代码来处理每种情况。但是例如,请看一下HttpClientErrorException子层次结构。您只有一个例外来映射任何4xx类型的错误。如果您需要深入,那么可以。但是仅捕获HttpClientErrorException,就可以处理将错误数据提供给服务的任何情况。
DefaultResponseErrorHandler非常简单可靠。如果响应状态代码不是2xx系列,则它对hasError方法返回true。
一个非常简单的解决方案可以是:
try {
requestEntity = RequestEntity
.get(new URI("user String"));
return restTemplate.exchange(requestEntity, String.class);
} catch (RestClientResponseException e) {
return ResponseEntity.status(e.getRawStatusCode()).body(e.getResponseBodyAsString());
}
Run Code Online (Sandbox Code Playgroud)
我已处理如下:
try {
response = restTemplate.postForEntity(requestUrl, new HttpEntity<>(requestBody, headers), String.class);
} catch (HttpStatusCodeException ex) {
response = new ResponseEntity<String>(ex.getResponseBodyAsString(), ex.getResponseHeaders(), ex.getStatusCode());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
130891 次 |
| 最近记录: |