簡體   English   中英

錯誤沒有從被叫服務傳播到呼叫者服務

[英]Error not getting propagated from called service to caller service

我們有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()返回類似“在對HTTP的PUT請求上發生I / O錯誤……”的方法,我可以在調用者服務級別捕獲錯誤消息嗎?

這兩個服務都是SpringBoot應用程序,並具有RestControllers

您正在呼叫者服務中處理異常,但在被叫服務中,它也需要發送異常。 對於泛型,您可以引用此名稱或具有自己的自定義隱式。

被叫服務:

@Getmapping("/uri")
public ResponseEntity<Object> getIt(@RequestBody MyRequest req) {
try{
//...business logic

    return new ResponseEntity<>(response, HttpStatus.OK);

}catch(MyException e){
    return new ResponseEntity<>(e, HttpStatus.SOME_EXC_STATUS); //or custom exc
}catch(Exception e){
    return new ResponseEntity<>(e, HttpStatus.SOME_EXC_STATUS);
}
}

這樣,只有您才能獲得異常狀態。

被調用的服務需要在響應中發送它。 您正在打個電話。 如果這是一種方法,那么您也可能從調用方那里捕獲了它​​。

在被叫服務的響應實體中傳遞異常消息。 使用控制器建議或responsestatusexception。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM