【问题标题】:Spring RestTemplate Statuscodeexception pass custom message to exception handlingSpring RestTemplate Statuscodeexception 将自定义消息传递给异常处理
【发布时间】:2019-11-07 09:19:46
【问题描述】:

在我的应用中,服务 A 与服务 B 通信。 在服务 B 中,我做了一些 db 查询,因此可能会出现 NOT_FOUND 状态。

代码:

服务A

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 的异常处理中使用“我的错误消息”。有没有办法将它传递给异常?

【问题讨论】:

    标签: java spring rest exception resttemplate


    【解决方案1】:

    HttpStatusCodeException 继承了一个名为getResponseBodyAsString 的方法,只需使用e.getResponseBodyAsString() 即可获得Service B 发送的自定义消息。

    【讨论】:

    • 非常感谢。正是我想要的
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2019-06-02
    • 1970-01-01
    • 2013-07-05
    • 2021-07-10
    • 1970-01-01
    • 2020-05-26
    • 2014-09-28
    相关资源
    最近更新 更多