httpservererrorexception.internalservererror不会从http请求rethtemplate捕获错误500
我目前正在开发一项服务,该服务访问另一个团队的另一个服务,该服务在服务没有数据时给出了响应代码500。首先,我可以使用httpservererrorexception.internalsererrorror
来捕获异常并获得响应主体。但是,这并不像以前那样起作用。该错误并未被该异常捕获,当我尝试使用catch(异常E)
时,它也没有抓住并让它通过它使服务无法正常运行。.
以下是代码,什么是实际上从下面的代码中错误?我应该如何处理500个响应代码?
Map resultMap1;
try {
resultMap1 = restTemplate.postForEntity(url, request, Map.class);
} catch (HttpServerErrorException.InternalServerError e) {
resultMap1 = new HashMap();
resultMap1.put("errorMessage", e.getMessage());
return resultMap1;
} catch (HttpServerErrorException.GatewayTimeout e) {
throw new AppException(
GATEWAY_TIMEOUT_CD,
GATEWAY_TIMEOUT_MSG +": "+ url,
null
);
} catch (HttpServerErrorException.BadGateway e) {
throw new AppException(
BAD_GATEWAY_CD,
BAD_GATEWAY_MSG +": "+ url,
null
);
} catch (HttpServerErrorException.ServiceUnavailable e) {
throw new AppException(
SERVICE_UNAVAILABLE_CD,
"The service "+ url +" was unavailable.",
null
);
}
Map returnMap = new HashMap(); // this is to be the result from my service
switch (resultMap.get("content_type")){ // In this one, I get NullPointerException
// Here would be selection how the content should be treated, different content have different treatment
}
我知道实际上可以通过添加其他情况或其他情况来处理NullPoInterException,但是,我很好奇为什么例外情况不会像预定的那样被抓住。
I am currently working on a service that hit another service from another team, which gives the response code 500 when the service has no data to be sent. At first, I can utilize the HttpServerErrorException.InternalServerError
to catch the exception and get the response body. However, this did not work as it used to. That error was not caught by that Exception and when I tried using catch (Exception e)
, it also caught nothing and let it through that makes the service not running correctly..
Below is the code, what is actually wrong from my code below? How should I handle that 500 response code?
Map resultMap1;
try {
resultMap1 = restTemplate.postForEntity(url, request, Map.class);
} catch (HttpServerErrorException.InternalServerError e) {
resultMap1 = new HashMap();
resultMap1.put("errorMessage", e.getMessage());
return resultMap1;
} catch (HttpServerErrorException.GatewayTimeout e) {
throw new AppException(
GATEWAY_TIMEOUT_CD,
GATEWAY_TIMEOUT_MSG +": "+ url,
null
);
} catch (HttpServerErrorException.BadGateway e) {
throw new AppException(
BAD_GATEWAY_CD,
BAD_GATEWAY_MSG +": "+ url,
null
);
} catch (HttpServerErrorException.ServiceUnavailable e) {
throw new AppException(
SERVICE_UNAVAILABLE_CD,
"The service "+ url +" was unavailable.",
null
);
}
Map returnMap = new HashMap(); // this is to be the result from my service
switch (resultMap.get("content_type")){ // In this one, I get NullPointerException
// Here would be selection how the content should be treated, different content have different treatment
}
I know that the NullPointerException can actually be handled by adding ELSE or another case, However, I curious about why the Exception wasn't caught well as it supposed to be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论