我使用下面的代码来调用远程API来删除用户id(http://localhost:8080/remove)。
try {
final RestTemplate restTemplate = new RestTemplate();
final UriComponentsBuilder builder =
UriComponentsBuilder.fromUriString(url);
builder.queryParam("acdc_id", acdcId);
ResponseEntity<ServiceResponse> result =
restTemplate.exchange(
builder.toUriString(),
HttpMethod.DELETE,
null,
ServiceResponse.class);
}catch(Exception e){
//exception handling
}
对于成功流程,远程API返回200 http代码(工作正常),但当某些用户id不可用时,API将发送以下自定义响应:
{
"error code": "404",
"error": "USER ID Node not found : xyz"
}
我已经有了ServiceResponse.java类来得到上面的响应,但在这种情况下Rest模板返回了下面的错误。
org.springframework.web.client.HttpClientErrorException$NotFound: 404 null
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)
我的ServiceResponse类是,
@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceResponse {
@JsonProperty(value = "error code")
private String errorCode;
@JsonProperty(value = "error")
private String error;
/**
* @return the error.
*/
public String getError() {
return error;
}
/**
* @param error The error to set.
*/
public void setError(final String error) {
this.error = error;
}
/**
* @return the errorCode.
*/
public String getErrorCode() {
return errorCode;
}
/**
* @param errorCode The errorCode to set.
*/
public void setErrorCode(final String errorCode) {
this.errorCode = errorCode;
}
}
您能否在这里帮助我解决我的问题,或提供任何建议,我如何才能从API获得正确的响应而不是空错误
发布于 2020-01-23 15:49:06
正如我在comment中提到的,您得到的是HttpClientErrorException,它应该被捕获并处理。您将捕获整个异常类,但其中没有任何代码。或者,您也可以将@ControllerAdvice和@ExceptionHandler一起使用来实现这一点。
发布于 2020-01-23 15:46:46
您可以使用控制器建议@ControllerAdvice
注释来处理异常。您可以通过该方法发送自定义响应。您可以为HttpClientErrorException定义exceptionHandler,并从此方法发送自定义响应。
有关更多详细信息,请查看https://www.tutorialspoint.com/spring_boot/spring_boot_exception_handling.htm。
另一种选择是您可以使用CustomResponseErrorHandler,如下所示
@Component
public class RestTemplateResponseErrorHandler
implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse httpResponse)
throws IOException {
return (
httpResponse.getStatusCode().series() == CLIENT_ERROR
|| httpResponse.getStatusCode().series() == SERVER_ERROR);
}
@Override
public void handleError(ClientHttpResponse httpResponse)
throws IOException {
if (httpResponse.getStatusCode()
.series() == HttpStatus.Series.SERVER_ERROR) {
// handle SERVER_ERROR
} else if (httpResponse.getStatusCode()
.series() == HttpStatus.Series.CLIENT_ERROR) {
// handle CLIENT_ERROR
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
throw new NotFoundException();
}
}
}
}
然后像这样使用它
@Bean
RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
return restTemplate;
}
请检查ResponseErrorHandler的https://www.baeldung.com/spring-rest-template-error-handling
https://stackoverflow.com/questions/59873432
复制相似问题