zoukankan      html  css  js  c++  java
  • SpringMVC中@RestControllerAdvice统一异常处理

    GlobalExceptionHandler为统一异常处理类,MyException自定义异常类,
    @RestControllerAdvice相当于Controller的切面,对异常统一处理,定制,这样更好返回给前端。
    @RestControllerAdvice
    public class GlobalExceptionHandler {
        public GlobalExceptionHandler(){}
        @ExceptionHandler({MyException.class})
        public ResponseEntity<ResErr> handleMyException(MyException ex){
            ResErr resErr=new ResErr();
            resErr.setErrCode(12344);
            resErr.setErrMsg(ex.getMessage());
            return new ResponseEntity<>(resErr, HttpStatus.OK);
        }
        @ExceptionHandler({RuntimeException.class})
        public ResponseEntity<ResErr> handleMyException(RuntimeException ex){
            ResErr resErr=new ResErr();
            resErr.setErrCode(99999);
            resErr.setErrMsg("运行时异常");
            return new ResponseEntity<>(resErr, HttpStatus.OK);
        }
        @ExceptionHandler({Exception.class})
        public ResponseEntity<ResErr> handleMyException(Exception ex){
            ResErr resErr=new ResErr();
            resErr.setErrCode(99999);
            resErr.setErrMsg("系统异常");
            return new ResponseEntity<>(resErr, HttpStatus.OK);
        }
    }
  • 相关阅读:
    poj 2674 Linear world
    poj 3185 The Water Bowls
    The Largest Clique (uva11324)
    Proving Equivalences (LA 4287)
    强联通分量( HihoCoder 1185 )
    求点双联通分量(HihoCoder
    求桥,割点(HihoCoder
    欧拉回路
    uva10054
    表达式树(公共表达式消除 uva 12219)
  • 原文地址:https://www.cnblogs.com/mufeng07/p/12659441.html
Copyright © 2011-2022 走看看