我無法理解ExceptionHandlerExceptionResolver拋出例外的原因。我撰寫了一個自定義@RestControllerAdvice ExceptionHandler 來捕獲我的 Spring Boot 應用程式拋出的例外。捕獲后,我的 ExceptionHandler 作為回應回傳來自拋出例外的訊息。但是我仍然從ExceptionHandlerExceptionResolver收到一條日志訊息,我不知道為什么。
這里的日志訊息:
2022-05-04 10:08:53.043 INFO 17600 --- [nio-8080-exec-3] at.sds.wm.common.CommonExceptionHandler : Equipment Barbell 660Kg exists already. Either id already taken or name combined with same equipment already saved
2022-05-04 10:08:53.049 WARN 17600 --- [nio-8080-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [at.sds.wm.equipment.exceptions.EquipmentExistsAlreadyException: Equipment Barbell 660Kg exists already. Either id already taken or name combined with same equipment already saved]
我首先想到的是,呼叫ExceptionHandlerExceptionResolver是因為我的ExceptionHandler拋出了一個例外,但事實并非如此。
關于如何抑制第二個日志的任何提示和技巧,或者有人可以解釋我為什么呼叫ExceptionHandlerExceptionResolver嗎?
在我呼叫的 ExceptionHandler 部分的代碼片段下方:
@ExceptionHandler(value = {WorkoutExistsAlreadyException.class, EquipmentExistsAlreadyException.class, ExerciseExistsAlreadyException.class})
@ResponseStatus(code = HttpStatus.CONFLICT)
public String handleExistsAlreadyException(RuntimeException ex) {
log.info(ex.getMessage());
return ex.getMessage();
}
uj5u.com熱心網友回復:
這與應用程式屬性有關spring.mvc.log-resolved-exception。這在默認情況下設定為 false,spring-boot但如果您使用spring-boot-devtools,則將其切換為 true。
因此,在這種情況下,您必須宣告spring.mvc.log-resolved-exception = false例外處理程式決議器以停止撰寫有關例外的警告。
參考的 Spring Boot 檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/469980.html
上一篇:Java中的陣列搜索例外
下一篇:如何將功能與輸入和除外結合起來
