zoukankan      html  css  js  c++  java
  • spring mvc异常统一处理(ControllerAdvice注解)

    @ControllerAdvice  
    public class GlobalExceptionHandler {  
          
        private final static AsJEELogger LOG = AsJEELoggerFactory.getLogger(GlobalExceptionHandler.class);  
          
        private final static String EXPTION_MSG_KEY = "message";  
          
        @ExceptionHandler(BusinessException.class)  
        @ResponseBody  
        public void handleBizExp(HttpServletRequest request, Exception ex){  
            LOG.info("Business exception handler  " + ex.getMessage() );  
            request.getSession(true).setAttribute(EXPTION_MSG_KEY, ex.getMessage());  
        }  
          
        @ExceptionHandler(SQLException.class)  
        public ModelAndView handSql(Exception ex){  
            LOG.info("SQL Exception " + ex.getMessage());  
            ModelAndView mv = new ModelAndView();  
            mv.addObject("message", ex.getMessage());  
            mv.setViewName("sql_error");  
            return mv;  
        }  
      
    }  
    
    public class BusinessException extends Exception{  
      
        private static final long serialVersionUID = 1L;  
          
        //业务类型  
        private String bizType;  
        //业务代码  
        private int bizCode;  
        //错误信息  
        private String message;  
          
        public BusinessException(String bizType, int bizCode, String message){  
            super(message);  
            this.bizType = bizType;  
            this.bizCode = bizCode;  
            this.message = message;  
        }  
      
        public BusinessException(String message){  
            super(message);  
            this.bizType = "";  
            this.bizCode = -1;  
            this.message = message;  
        }  
      
        public BusinessException(String bizType, String message){  
            super(message);  
            this.bizType = bizType;  
            this.bizCode = -1;  
            this.message = message;  
        }  
          
        public BusinessException(int bizCode, String message){  
            super(message);  
            this.bizType = "";  
            this.bizCode = bizCode;  
            this.message = message;  
        }  
      
        public String getBizType() {  
            return bizType;  
        }  
      
        public void setBizType(String bizType) {  
            this.bizType = bizType;  
        }  
      
        public int getBizCode() {  
            return bizCode;  
        }  
      
        public void setBizCode(int bizCode) {  
            this.bizCode = bizCode;  
        }  
      
        public String getMessage() {  
            return message;  
        }  
      
        public void setMessage(String message) {  
            this.message = message;  
        }  
      
    }  
    
    
    @Controller  
    @RequestMapping("/security/user")  
    public class UserController  extends AbstractController<User>{  
      
        @Resource  
        private UserService userService;  
        @Resource  
        private ServiceFacade serviceFacade;  
      
        @RequestMapping("login")  
        public String login() {   
            return "login";  
        }  
          
        @RequestMapping("login2")  
        public String login2() throws Exception {  
            throw new SQLException("出错鸟。。。。。。。。。");  
        }     
          
        @RequestMapping("login3")  
        public String login3() throws Exception {   
            throw new BusinessException("业务执行异常");  
        }  
          
        //此方法抛出的异常不是由GlobalExceptionHandler处理  
        //而是在catch块处理  
        @RequestMapping("login4")  
        public String login4() {   
            try {  
                throw new BusinessException("业务执行异常");  
            } catch (BusinessException e) {  
                e.printStackTrace();  
            }  
            return "login";  
        }  
          
    }  
  • 相关阅读:
    C#遍历指定路径下的文件夹
    ArcEngine的拓扑分析之ITopologicalOperator
    ArcEngine的拓扑分析之ITopologicalOperator
    输出旋转方形数字图形
    hdu4861(游戏)
    动态规划解决最长公共子序列问题(转)
    求解概率的坑题
    最后一周第二天训练赛之第二题
    最后一周训练赛第一题
    洛谷—— P2690 接苹果
  • 原文地址:https://www.cnblogs.com/raphael5200/p/7514693.html
Copyright © 2011-2022 走看看