gpt4 book ai didi

java - 如果 HTTPStatus Code 不等于 200,则响应实体为 null

转载 作者:行者123 更新时间:2023-12-01 16:50:17 25 4
gpt4 key购买 nike

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

如果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

最佳答案

RestClientException没有诸如 getStatusCode()getResponseBodyAsString() 之类的方法。因此,我无法在 catch block 中找到它们。

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

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
}

关于java - 如果 HTTPStatus Code 不等于 200,则响应实体为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61703921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com