1.采用方式
这里通过 @ControllerAdvice和@ExceptionHandler 的异常处理方式
2.具体步骤
1.创建统一异常处理类(GlobalExpetion)
2.定义判断是否是Aajx请求的方法
public boolean isAjax(HttpServletRequest request) {
return (request.getHeader("X-Requested-With") != null &&
"XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
}
3.不是ajax请求的异常方法
public ModelAndView exceptionPD(Exception ex,HttpServletRequest request) {
ModelAndView modelAndView=new ModelAndView();
if(ex instanceof UnauthenticatedException) {
modelAndView.addObject("msg", "认证时间已过,请重新登录");
modelAndView.addObject("login",0);
}
else if(ex instanceof UnauthorizedException) {
modelAndView.addObject("msg", "你还没有该访问权限!!!请联系管理员吧");
}
else if(ex instanceof Exception){
modelAndView.addObject("msg", "系统异常");
}
modelAndView.setViewName(DEFAULT_ERROR_VIEW);
return modelAndView;
}
4.ajax异常处理方法
public JSONResult ajaxException(Exception ex,HttpServletRequest request,HttpServletResponse response) {
//自定义的返回数据类
JSONResult json=new JSONResult();
if(ex instanceof UnauthenticatedException) {
json.setStatus(401);
json.setMsg("未认证或者session过期");
Map<String, Object> map=new HashMap<>();
map.put("title","是否重新登录");
map.put("url","/login");
}
else if(ex instanceof UnauthorizedException) {
json.setMsg("没有权限");
json.setStatus(403);
Map<String, Object> map=new HashMap<>();
map.put("title","是否切换账户,重新登录");
map.put("url","/login");
}
else if(ex instanceof Exception){
json.setStatus(500);
json.setMsg("系统异常");
}
return json;
}
最后在异常接受中判断什么异常,是不是ajax请求,再决定怎么跳转
@ExceptionHandler(Exception.class)
@ResponseBody
public Object errorHandler(HttpServletRequest request,
HttpServletResponse response, Exception ex) {
ex.printStackTrace();
//是不是ajax请求
if(isAjax(request)){
//传递异常,调用ajaxException方法
return ajaxException(ex, request, response);
}
else {
//不是异步请求
return exceptionPD(ex,request);
}
}
3.完整代码
package com.xyz.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import com.xyz.util.JSONResult;
@ControllerAdvice
public class GlobalExpetion {
public static final String DEFAULT_ERROR_VIEW = "error/error";
@ExceptionHandler(Exception.class)
@ResponseBody
public Object errorHandler(HttpServletRequest request,
HttpServletResponse response, Exception ex) {
ex.printStackTrace();
if(isAjax(request)){
return ajaxException(ex, request, response);
}
else {
//不是异步请求
return exceptionPD(ex,request);
}
}
public boolean isAjax(HttpServletRequest request) {
return (request.getHeader("X-Requested-With") != null &&
"XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
}
public ModelAndView exceptionPD(Exception ex,HttpServletRequest request) {
ModelAndView modelAndView=new ModelAndView();
if(ex instanceof UnauthenticatedException) {
modelAndView.addObject("msg", "认证时间已过,请重新登录");
modelAndView.addObject("login",0);
}
else if(ex instanceof UnauthorizedException) {
modelAndView.addObject("msg", "你还没有该访问权限!!!请联系管理员吧");
}
else if(ex instanceof Exception){
modelAndView.addObject("msg", "系统异常");
}
modelAndView.setViewName(DEFAULT_ERROR_VIEW);
return modelAndView;
}
public JSONResult ajaxException(Exception ex,HttpServletRequest request,HttpServletResponse response) {
JSONResult json=new JSONResult();
if(ex instanceof UnauthenticatedException) {
json.setStatus(401);
json.setMsg("未认证或者session过期");
Map<String, Object> map=new HashMap<>();
map.put("title","是否重新登录");
map.put("url","/login");
}
else if(ex instanceof UnauthorizedException) {
json.setMsg("没有权限");
json.setStatus(403);
Map<String, Object> map=new HashMap<>();
map.put("title","是否切换账户,重新登录");
map.put("url","/login");
}
else if(ex instanceof Exception){
json.setStatus(500);
json.setMsg("系统异常");
}
return json;
}
}
注意:
1.JSONResult 类是我自已定义的消息返回类。
2.根据实际情况修改代码,这只是适用于当前项目
3.对异常的拦截这一块可以根据你自己的需求去拦截,注意普通异常和异步异常的返回消息不同
4.有不好的地方欢迎指点。
如有疑问可以留言交流!!!!!!!!!