繁体   English   中英

返回ResponseEntity和Http错误时,RestTemplate.postForObject返回Null

[英]RestTemplate.postForObject Returns Null When ResponseEntity and Http Error Are Returned

我调用后端服务来获取PersonResponse对象:

PersonResponse response = restTemplate.postForObject(url, request, PersonResponse.class);

PersonResponse类包含一个“状态”字段,用于指示是否成功从后端检索了一个人的信息:

 public class PersonResponse {
    private String name;
    private String address;
    private ResponseStatus status;
     ......
 }

 public class ResponseStatus {
    private String errorCode;
    private String errorMessage;
     ......
 }

因此,当成功检索到响应(http 200)时,我可以取回PersonResponse类型的响应。 但是,当出现错误(400或500)时,后端仍然会向我返回一个PersonResponse,但是只有“ status”字段中填充了错误信息,这就是后端如何向我返回响应:

 backend code:

 PersonResponse errResp = .....; // set the status field with error info
 return new ResponseEntity<PersonResponse>(errResp, HttpStatus.INTERNAL_SERVER_ERROR); 

但是下面的呼叫返回了一个空响应,尽管它应该给我一个PersonResponse错误信息。 有人可以让我知道为什么吗?

try {
PersonResponse response = restTemplate.postForObject(url, request, PersonResponse.class);
} catch (HttpStatusCodeException se) {
  log.debug(se.getResponseBodyAsString()); 
  // I was able to see the error information stored in PersonResponse in the log
}        
return response;  // always null when 500 error is thrown by the backend

请阅读以下内容:

默认情况下,如果发生HTTP错误, RestTemplate将抛出以下exceptions之一:

HttpClientErrorException

–如果HTTP状态为4xx

HttpServerErrorException

–如果HTTP状态为5xx

UnknownHttpStatusCodeException

–如果HTTP状态未知

所有这些exceptions都是RestClientResponseException扩展。

现在,由于您的后端响应为5xx(在您的情况下为500),因此对于您的客户端RestTemplate它是HttpServerErrorException

此外, response你与接收HTTP 500 (INTERNAL SERVER ERROR)状态, RestTemplate不会映射与POJO /反序列化回,因为它不再是一个成功( HTTP 200 )响应,即使后端包裹的errorCode和消息状态。

因此,在您的情况下,始终为null

现在根据您的需求(我想从您的原始帖子中得出的信息),即使处于4xx或5xx状态,您也要返回ResponseEntity。 您可以为各个catch块实现此功能,例如:

try {
PersonResponse response = restTemplate.postForObject(url, request, PersonResponse.class);
} catch (HttpStatusCodeException se) {
  log.debug(se.getResponseBodyAsString()); 
  // I was able to see the error information stored in PersonResponse in the log
// Here you have to implement to map the error with PersonResponse 
 ResponseStatus  errorStatus = new ResponseStatus();
 errorStatus.setErrorCode(HTTP500);
 errorStatus.setErrorMessage(YOURMESSAGEFROMERROR);
 PersonResponse  responseObject = new PersonResponse();
 responseObject.setResponseStatus(errorStatus);
 return new ResponseEntity<PersonResponse>(responseObject,HTTPStatus.200Or500); // you can design as you need 200 or 500
 } catch (HttpClientErrorException ex){
   //same way for HTTP 4xx 
}

另外,还有一些其他的方式就像这样 :在您使用SpringExceptionHandler,并集中处理程序你决定如何,如果你从后台收到4XX或者5XX从客户端响应。 最后,这完全取决于您对系统的设计方式,因为您说您无法控制后端,因此您必须根据后端响应在客户端上实现事情。

希望这可以帮助。

您应该处理HttpClientErrorException ,还尝试将服务返回语句更改为ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errResp)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM