您的位置:首页 > 编程语言 > Java开发

spring mvc异常统一处理(ControllerAdvice注解)

2017-10-26 16:37 681 查看
http://blog.csdn.net/chenaschen/article/details/5129156

@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;  

    }  

  



6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: