@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()));
}
}
原文:https://www.cnblogs.com/taiguyiba/p/10235879.html