【问题标题】:Response status code showing 200 for gateway error (504)响应状态代码显示 200 网关错误 (504)
【发布时间】: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 块?

提前致谢。

【问题讨论】:

    标签: java http-status-code-504


    【解决方案1】:

    如果出现错误,您的方法似乎返回空响应。你能检查一下你是否在调用者中做了任何处理并从控制器层本身传递了 200。

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多