【发布时间】:2022-01-27 14:50:22
【问题描述】:
我有一个调用第三方 API 的 Java Springboot 微服务。对于我的一个测试用例,API 返回 422 Unprocessable entity with a custom Response JSON Body
{
"request_status": "error",
"status": "error",
"requested_url": "www.example.de",
"url": "http://www.newexample.de/",
"issue": "redirect",
"recommendedUrl": true,
"error_message": "The web address you entered redirects to another website."
}
我无法在我的微服务中检索此响应正文。我的代码如下所示
try {
HttpEntity<Input_Class> httpentity = new HttpEntity<Input_Class>( inputClassObj, headers);
responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpentity,
new ParameterizedTypeReference<Output_Class>() {});
} catch(RestClientException ue) {
if (ue instanceof HttpStatusCodeException) {
String errorResponse = ((HttpStatusCodeException) ue).getResponseBodyAsString();
logger.info(errorResponse);
}
我在日志中看到的errorResponse是
{
"status": "ERROR",
"errorMessage": "Unprocessable Entity"
}
谁能告诉我应该使用哪个异常类来获取 API 发送的响应正文。
【问题讨论】:
-
你试过
RestClientResponseException而不是RestClientException吗?
标签: java spring-boot rest maven