首页 > 编程语言 > 详细

Springboot全局异常处理

时间:2019-01-07 22:16:29      阅读:168      评论:0      收藏:0      [点我收藏+]
@ControllerAdvice
public class MyControllerAdvice {
 
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest request,HttpServletResponse response,Exception e) throws Exception{
     
   e.printStackTrace(); if(isAjax(request)){
      return JSONResult.errorException(e.getMessage());
     }else{
        ModelAndView mav = new ModelAndView();
mav.addObject("Exception",e);
        mav.addObject("url",request.getRequestURL);
   mav.setViewName(ERROR_VIEW);
return mav;
      }
}
  public static boolean isAjax(HttpServletRequest request){    

    return(httpRequest.getHeader("
X-Requested-With") != null && "XMLHttpRequest".equalsIgnoreCase(((HttpServletRequest) request).getHeader("X-Requested-With").toString()));

  }
 
}

 

Springboot全局异常处理

原文:https://www.cnblogs.com/taiguyiba/p/10235879.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!