【发布时间】:2020-06-08 23:22:21
【问题描述】:
我有一个 REST 端点,它调用另一个 API,当我通过 Rest 客户端(失眠)进行验证时,该 API 需要一段时间来处理并返回 504 错误。但在我的服务中,我将此交易视为成功 200 而不是 504。
下面是我的代码 sn-p:
public ResponseEntity<Customer> processResponse(Customer customer, String restUri) {
ResponseEntity<Customer> response;
String customerJson = null;
try {
RestTemplate restTemplate = restTemplateBuilder.basicAuthorization(userName, password).build();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Customer> entity = new HttpEntity<>(customer, headers);
CustomerJson = jacksonUtil.toJSON(customer);
response = restTemplate.exchange(restUri, HttpMethod.PUT, entity, Customer.class);
if (response.getStatusCode().is2xxSuccessful()) {
logger.info("Return success from the server");
} else {
logger.error("Error while getting the response from the server");
}
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
return response;
}
我在这里缺少什么?为什么它不执行 else 块?
提前致谢。
【问题讨论】: