我正在研究 SpringBootApplication。在這個應用程式中,我有 4 個使用 Feign 在彼此之間進行通信的微服務。在控制器中,我有一段如下所示的代碼,用于捕獲例外并將其回傳到視圖,以防出現問題。
try {
patientDTO = patientProxyFeign.getPatientById(id);
noteDTOList = historyProxyFeign.getAll(id);
assessmentDTO = assessmentProxyFeign.getPatientAssessment(id);
} catch (Exception e) {
log.error("" e.getMessage());
model.addAttribute("errorMsg", e.toString());
return "error/error";
}
如果有例外,我會在視圖中收到這樣的訊息:
feign.FeignException$NotFound: [404] during [GET] to [http://localhost:8081/patient/12000] [PatientProxyFeign#getPatientById(Integer)]: [{"timestamp":"2021-12-16T16:21:27.790 00:00","status":404,"error":"Not Found","path":"/patient/12000"}]
我想要做的是只獲取狀態代碼和“未找到”訊息。
有人知道怎么做嗎?(在 google 上搜索,但似乎太具體了。我可能沒有使用正確的關鍵字。)
uj5u.com熱心網友回復:
您可以通過呼叫獲取狀態e.status(),然后您可以switch-case狀態并基于此獲取訊息。您還可以構建一個Mapof<Integer, String>并按狀態獲取訊息。要更多地了解 FeignException,請訪問https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/FeignException.java
強烈建議您具體說明您的內容catch:
} catch (FeignException e) {
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/383855.html
上一篇:僅更改bash腳本中第一行的輸出
下一篇:例外未處理的彈出視窗
