08整合后高级配置-统一异常处理拦截器

2019-08-06  本文已影响0人  RobertLiu123

目标

第1节

统一异常处理

目的:有一些能识别出的异常,我们要对他进行分类。产生出我们的自定义异常。对于运行期不明确的异常,我们也要捕捉到,转化成统一的异常。从用户的角度始终看到的是处理后的异常

第一步:编写几种自定义异常

1.AppException 2.BusinessException 3.DBException.....

新建包:com.neuedu.utils
新建异常类:AppException。java
package com.neuedu.utils;

public class AppException extends Exception {
    private String message;
    private int code;

    public AppException(String message, int code) {
        super();
        this.message = message;
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

}

第二步:编写自定义异常处理器

系统能够捕捉到的异常,我们可以归类到自定义异常里,但是如果捕捉不到的,会有一个异常处理器的可以捕捉到,他总的异常处理器时,我们把那些程序没有捕捉到的异常再转换成自定义异常。这样用户就不会收到不明异常了。

public class ExceptionResovler implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
            Exception ex) {
        //判断是不是我们自定义异常
        AppException appException=null;
        if(ex instanceof AppException){
            //1)如果ex是自定义的异常(controller,service,dao抛出),
            //这种异常,把异常信息反馈到客户端即可
            appException=(AppException)ex;
        }else{
            //2)如果不是我们自定义的异常,意味着他是未知异常。
            //这种异常我们再封装成自定义异常。再返回到客户端
            appException=new AppException("未知异常",-1);
        }
        ModelAndView mav=new ModelAndView("error");
        mav.addObject("error", appException);
        return mav;
    }
}

第三步:配置使用异常处理器(解析器)

 <!-- 配置异常处理器 -->
<bean class="com.neuedu.utils.ExceptionResovler"/>

第四步:开发错误处理页面

<body>
${error.message}
</body>

第五步:测试

  int i=1/0;//测试未知异常
  //测试自定义异常
  if(1>0){
  throw new AppException("查询异常",999);
  }

拦截器

https://www.cnblogs.com/xiangkejin/p/9368984.html
https://www.cnblogs.com/panxuejun/p/7715917.html

拦截器:类似过滤器,但是他可以针对不同时机进行拦截,比如事前、事后拦截
定义拦截器:
实现HandlerInterceptor接口
接口中提供三个方法:
preHandle(事前) :请求没有到达controller前
postHandle(已到达controller,视图渲染之前被调用, 还可以修改ModelAndView)
afterCompletion(事后):统一异常,统一日志处理

开发步骤:
1)编写拦截器类,写其中的方法

package com.neuedu.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
        throws Exception {
    //事后
    System.out.println("事后拦截");
}

@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
        throws Exception {
    // 事中拦截
    System.out.println("事中拦截");
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
    // 事前拦截
    System.out.println("事前拦截");
    //做登录验证
    //拿到sesssion
    HttpSession session=request.getSession();
    //判断session里有没有用户信息,如果有,就放过去,否则进登录页
    if(null==session.getAttribute("user")){
        System.out.println("重定向到登录页");
        return false;
    }
    return true;
}
}

2)springmvc.xml中增加拦截器配置

<!-- 所有配置拦截器 -->
 <mvc:interceptors>
       <!-- 配置一个拦截器开始 -->
      <mvc:interceptor>
           <!-- 拦截所有请求 -->
           <mvc:mapping path="/**"/>
      <!-- 以做身份认证为例,需要排除所有登录程序,也就是controller地址以/login开始的请求 -->
          <mvc:exclude-mapping path="/login/**"/>
          <bean class="com.neuedu.utils.MyInterceptor"></bean>
      </mvc:interceptor>
      <!-- 配置一个拦截器结束 -->
 </mvc:interceptors>

Spring 过滤器 拦截器 AOP区别

https://blog.csdn.net/dreamwbt/article/details/82658842

上一篇 下一篇

猜你喜欢

热点阅读