在我的应用服务A中与服务B通信。在服务b中,我做一些数据库查询,以便出现一个not_found状态。
代码:
服务
try{
RestTemplate.exchange()...
}catch(HttpStatusCodeException e){
return new ResonseEntity<String>(// Here custom message of service B extracted from e //,e.getStatusCode)
}
服务B:
return new ResponseEntity<String> ("My Error Message", HttpStatus.NOT_FOUND)
因此,基本上我想在服务A中使用"我的错误消息"。是否有办法将其传递给例外?
HttpStatusCodeException
继承了一种称为 getResponseBodyAsString
的方法,只需使用 e.getResponseBodyAsString()
即可获得由服务发送的自定义消息b。