如果HTTPStatus代码不等于200,则响应实体为null

问题描述 投票:0回答:2

我刚刚开始使用Spring框架并进行了一些测试。我正在尝试到达ResponseEntity。但是,如果HTTP状态代码不等于200,则ResponseEntity对象始终为null。因此,我无法到达状态代码或标题。

如果HTTP状态代码不等于200,有什么方法可以到达ResponseEntity?

public static String checkPatientExistence(String pNo, String eNo) throws RestClientException, NullPointerException {
    String result = null;
    RestTemplate restTemplate = createRestTemplate();
    final String uri = apiURL + "GetInstance";

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri)
            .queryParam("pNo", pNo)
            .queryParam("eNo", eNo);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);

    HttpEntity<String> requestEntity = new HttpEntity<>(headers);

    ResponseEntity<String> responseEntity = null;
    try {
        responseEntity = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.GET,
                requestEntity,
                String.class
        );
    } catch (RestClientException restClientException) {
        System.out.println(restClientException.getMessage());
        System.out.println(Arrays.toString(restClientException.getStackTrace()));
    }

    if (responseEntity != null) {
        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            result = "true";
        } else if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
            System.out.println(responseEntity.getStatusCode().toString());
            System.out.println(responseEntity.getBody());
            result = "false";
        } else if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
            System.out.println(responseEntity.getStatusCode().toString());
            System.out.println(responseEntity.getBody());
            result = "noContent";
        }
    } else if (responseEntity == null) {
        System.out.println("responseEntity is null");
        result = "false";
    }
    return result;
}

这是方法的输出:

400 Bad Request: [{"Message":"error message"}]
[org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101),
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:170),
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112),
org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63),
org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782),
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740),
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674),
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583),
com.xxx.xxx.App.checkPatientExistence(App.java:269), com.xxx.xxx.App.main(App.java:43)]
responseEntity is null
false

java spring resttemplate
2个回答
0
投票

您已经在catch代码块中收到状态码为200以外的响应。在catch块内定义您的ResponseEntity对象。

    catch (HttpStatusCodeException exception) {
              //Add data to responseEntity
        }

0
投票

RestClientException没有类似getStatusCode()getResponseBodyAsString()的方法。因此,我无法在catch块中到达它们。

因此,我为HttpsStatusCodeException添加了另一个catch块,并颠倒了catch块的顺序。

HttpStatusCodeException

extends RestClientResponseException

RestClientResponseException

extends RestClientException

这是我们可以使用的示例方法。

} catch (HttpStatusCodeException httpStatusCodeException) {
    System.out.println(httpStatusCodeException.getStatusCode());
    System.out.println(httpStatusCodeException.getResponseBodyAsString());
    System.out.println(httpStatusCodeException.getStatusText());
    System.out.println(httpStatusCodeException.getRawStatusCode());
    System.out.println(httpStatusCodeException.getMessage());
} catch (RestClientException restClientException) {
    // catch
}
© www.soinside.com 2019 - 2024. All rights reserved.