springboot @ExceptionHandler 异常 取消默认日志处理6金钱.anslist{backgr
技术问答
136 人阅读
|
0 人回复
|
2023-09-11
|
, c% y- ~0 [# p# _3 M; L
- @Slf4j
; m% p0 C, E; M* |5 Q8 A - @RestControllerAdvice
f5 h8 ]; n* i - public class GlobalExceptionHandler {# o* E& ] H) F: g$ C% m
- ) ^0 W8 o3 c1 V3 r6 N7 v
- @ResponseStatus(HttpStatus.BAD_REQUEST)
' J+ y5 v7 T6 O - @ExceptionHandler(RuntimeException.class)) l1 s5 j& x/ }$ z
- public AjaxResult handler(RuntimeException e) { T5 H0 W) t; y: _4 }
- // e.printStackTrace();& X \. |7 U+ Z7 d% a
- log.error("Assert异常:----------{}",e.getMessage());
* [7 Q0 S# u! o" y% A, i0 X" s$ G - return AjaxResult.fail(e.getMessage());/ z3 f1 U& X' e2 P: t5 x
- }
7 \, b# R5 S3 h - @ResponseStatus(HttpStatus.BAD_REQUEST)
) y+ T* j: x) D3 x7 j, L7 d0 F - @ExceptionHandler(IllegalArgumentException.class)/ w6 s/ C& u. p' t; R& I/ d
- public AjaxResult handler(IllegalArgumentException e) {0 k) [! x4 M9 p: m5 V
- log.error("Assert异常:----------{}",e.getMessage());( G0 X: J& V" |2 k3 f( w6 }
- return AjaxResult.fail(e.getMessage());
8 J+ b4 R6 h+ P/ a - }% @3 @' L' a/ W$ g
- }7 c7 c, A6 ~* |7 a G0 X
: @" n9 R9 q2 C3 T b, Q9 X* ?8 x' d5 j$ B7 F$ S( Y
- @RestController* f; c# k! k' F4 y8 e" l3 D
- public class TestController {
; t' X2 N+ J2 K* ?1 ?* [! n - 0 [9 C% ]* w' s2 ^) g9 T/ t5 q
- @Autowired1 r% w% j; B9 u/ A& f8 C% F* k# C
- private SysUserService sysUserService;
* x9 l( G7 y k) H" r3 ~% ]6 |/ X
' w. N- J5 k- N, t: K5 m- @GetMapping("/test1")
" S$ g Q; E' o) k- H: X - public Object test1() {
2 A- j6 v# R8 [4 V0 X" [, x0 x - int a = 10/0;1 v" O$ C4 L3 `8 m x0 Q
- return "1";
1 t& _* q* q" o/ q n. E - }9 ?; r( M: p6 n# t& p
) w2 W: Z# M0 W' ]3 q( o {- @GetMapping("/test2")
+ E8 b) X& c. i) \$ `! ? - public Object test2() {. [1 i) j1 S7 ?& e. h2 R
- return sysUserService.list();
/ G# N! r# n& e0 u - }
7 K5 O) c' b: \; r+ W% u - }
/ u* i4 y, @! F7 ?/ o& r8 { 如图所示 访问 test1 资源会报 by zero 错误 控制台会打印出 2条log记录+ N- \; e- A7 I6 \1 X
0 r2 T9 R+ ?* S3 m P- w' M7 W! s- error级别 log.error("Assert异常:----------{}",e.getMessage());warn级别 springboot 自带的log处理。
4 D- {- J: `) c. h: x 有没有大佬研究过怎么取消 springboot的 默认warn 提示
2 X: _# k3 p) P; i我知道答案 回答被采纳将会获得6 金钱 已有0人回答 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|
|
|
|
|