【发布时间】:2023-04-06 19:51:01
【问题描述】:
我们有 2 个具有 REST 控制器的服务。
调用者服务调用如下:
try {
response = restTemplate.exchange(urlModifyAttributes, HttpMethod.PUT, new HttpEntity<>(attributesMap, headerMap), parameterizedTypeReference, uriVars);
return response.getBody();
} catch (RestClientResponseException e) {
log.error("Modify attributes failed. Error {}", e.getMessage(), e);
throw new RuntimeException(e.getMessage());
} catch (Exception e) {
log.error("Modify attributes failed. Error: {}", e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
当被调用的服务抛出 RuntimeException 时:
throw new RuntimeException("Already modified");
但调用者服务无法使用 e.getMessage() 捕获错误消息“已修改”。而是 e.getMessage() 正在返回类似“I/O error on PUT request for http.......” 有没有办法可以在调用者服务级别捕获错误消息?
这两个服务都是 SpringBoot 应用程序并具有 RestControllers
【问题讨论】:
标签: java spring-boot error-handling runtimeexception