博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现自己的权限管理系统(四): 异常处理
阅读量:3890 次
发布时间:2019-05-23

本文共 4894 字,大约阅读时间需要 16 分钟。

一、定义请求url规范,方便我们做异常处理

这里我们要求项目中所有请求json数据,都使用.json结尾,页面请求都以.page结尾

二、定义一个Json数据处理类JsonData

作用:返回不同情况请求状态的信息:请求成功信息,请求失败信息

package com.mmall.common;import lombok.Getter;import lombok.Setter;import java.util.HashMap;import java.util.Map;//json处理类@Getter@Setterpublic class JsonData {    //true:正常处理 false:异常处理    private boolean ret;    //消息    private String msg;    //给前端返回的数据    private Object data;    public JsonData(boolean ret) {        this.ret = ret;    }    //请求成功返回数据和消息    public static JsonData success(Object object, String msg) {        JsonData jsonData = new JsonData(true);        jsonData.data = object;        jsonData.msg = msg;        return jsonData;    }    //请求成功只返回数据    public static JsonData success(Object object) {        JsonData jsonData = new JsonData(true);        jsonData.data = object;        return jsonData;    }    //请求成功只返回成功    public static JsonData success() {        return new JsonData(true);    }    //请求失败返回失败消息    public static JsonData fail(String msg) {        JsonData jsonData = new JsonData(false);        jsonData.msg = msg;        return jsonData;    }    //把请求结果封装到一个map中    public Map
toMap() { HashMap
result = new HashMap
(); result.put("ret", ret); result.put("msg", msg); result.put("data", data); return result; }}

三、定义自己的参数异常处理类ParamException,权限异常处理类 PermissionException

       PermissionException、ParamException都继承RuntimeException

package com.mmall.exception;public class ParamException extends RuntimeException {    public ParamException() {        super();    }    public ParamException(String message) {        super(message);    }    public ParamException(String message, Throwable cause) {        super(message, cause);    }    public ParamException(Throwable cause) {        super(cause);    }    protected ParamException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {        super(message, cause, enableSuppression, writableStackTrace);    }}
package com.mmall.exception;public class PermissionException extends RuntimeException {    public PermissionException() {        super();    }    public PermissionException(String message) {        super(message);    }    public PermissionException(String message, Throwable cause) {        super(message, cause);    }    public PermissionException(Throwable cause) {        super(cause);    }    protected PermissionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {        super(message, cause, enableSuppression, writableStackTrace);    }}

四、定义全局异常请求处理类

分三种情况:

  • 以.json请求失败
  • 以.page请求失败
  • 除了上面两种的其他请求失败

首先通过HttpServletRequest request 的 request.getRequestURL().toString()拿到请求的url,然后拿url做判断,并且把异常存在ModelAndView

备注: 使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图。从名字上看ModelAndView中的Model代表模型,View代表视图,这个名字就很好地解释了该类的作用。业务处理器调用模型层处理完用户请求后,把结果数据存储在该类的model属性中,把要返回的视图信息存储在该类的view属性中,然后让该ModelAndView返回该Spring MVC框架。框架通过调用配置文件中定义的视图解析器,对该对象进行解析,最后把结果数据显示在指定的页面上。 

package com.mmall.common;import com.mmall.exception.ParamException;import com.mmall.exception.PermissionException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//全局异常处理类public class SpringExceptionResolver implements HandlerExceptionResolver {    protected final Logger log = LoggerFactory.getLogger(this.getClass());    @Override    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {        String url = request.getRequestURL().toString();        ModelAndView mv;        String defaultMsg = "System error";        // 这里我们要求项目中所有请求json数据,都使用.json结尾,页面请求都以.page结尾        if (url.endsWith(".json")) {            //如果是我们自己定义的异常类型或参数异常            if (ex instanceof PermissionException || ex instanceof ParamException) {                JsonData result = JsonData.fail(ex.getMessage());                mv = new ModelAndView("jsonView", result.toMap());            } else {                log.error("unknown json exception, url:" + url, ex);                JsonData result = JsonData.fail(defaultMsg);                mv = new ModelAndView("jsonView", result.toMap());            }        } else if (url.endsWith(".page")){ // 这里我们要求项目中所有请求page页面,都使用.page结尾            log.error("unknown page exception, url:" + url, ex);            JsonData result = JsonData.fail(defaultMsg);            mv = new ModelAndView("exception", result.toMap());        } else {//既不是.json 也不是 .page结尾            log.error("unknow exception, url:" + url, ex);            JsonData result = JsonData.fail(defaultMsg);            mv = new ModelAndView("jsonView", result.toMap());        }        return mv;    }}

 

转载地址:http://ktphn.baihongyu.com/

你可能感兴趣的文章
Learning C with gdb
查看>>
不可不知的json库
查看>>
JSON格式解析和libjson使用简介
查看>>
关于Json格式的理解
查看>>
c语言解析json数据
查看>>
一个C实现的记日志的函数库
查看>>
C语言简单实现日志功能的的题目
查看>>
C 实现的 日志模块
查看>>
C语言实现简单的分级别写日志程序
查看>>
深入理解HTTP Session
查看>>
理解TCP中的三次握手
查看>>
linux session 浅谈
查看>>
Emacs 中文学习手册-1
查看>>
Emacs学习笔记(1):初学者的学习计划
查看>>
Emacs学习笔记(13):在Emacs中打开pdf
查看>>
Emacs学习笔记(14):在Emacs中使用git
查看>>
Emacs for vim Users---from http://www.crazyshell.org/blog/
查看>>
静态库和动态库链接那些事--http://www.crazyshell.org/blog/?p=50
查看>>
一年成为Emacs高手(像神一样使用编辑器) .--http://blog.csdn.net/redguardtoo/article/details/7222501
查看>>
GNU make 指南
查看>>