Springboot统一异常处理

Posted by youthred on April 15, 2021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器全局异常处理
 *
 * @author pj.w@qq.com
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object globalExceptionHandler(Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }

    @ResponseBody
    @ExceptionHandler(MyE.class)
    public Object globalExceptionHandler(MyE e) {
        e.printStackTrace();
        return e.getMessage();
    }
}