[英]Rest template result is getting null in api call in spring boot
我正在使用下面的代碼調用遠程 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 Template 返回以下錯誤。
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 獲得正確的響應而不是空錯誤
正如我在評論中提到的,您收到了 HttpClientErrorException,應該被捕獲並處理。 您正在捕獲整個 Exception 類,但其中沒有代碼。 或者您也可以一起使用@ControllerAdvice 和@ExceptionHandler 來實現這一點。
您可以使用 Controller Advice @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;
}
請檢查https://www.baeldung.com/spring-rest-template-error-handling的 ResponseErrorHandler
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.