springMVC的全局异常设置

先说为什么要设置全局异常。比如说,你程序出错了,500错误,大家都知道,程序出错就不会往下面执行,但是客户端那边还是一直等待状态,所以,我们后台无论正常还是报错都要给客户端返回数据。当然,我们可以try一下,程序还会往下面执行,但是不是所有的异常都需要try的。知道了重要性之后,就看看springmvc是如何实现了把

 

首先,我们需要一个new一个返回前端一个数据格式。这个是非常有必要的。以后我们返回前端的数据都是返回这个对象

JsonData.java
package com.mmall.common;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class JsonData {

    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;
    }

    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("ret", ret);
        result.put("msg", msg);
        result.put("data", data);
        return result;
    }
}
View Code

 

然后new一个自定义的异常。继承RuntimeException异常。不需要实现其他方法。默认调用父类的。这个异常的作用是指我们在业务逻辑中
遇到的异常信息,我们查看日志的时候会很清晰的发现,是那块业务逻辑出现的错误,而不是清一色的java提供的异常。
PermissionException.java
package com.mmall.exception;

/**
 * Created by 敲代码的卡卡罗特
 * on 2018/3/24 22:23.
 */
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);
    }
}
View Code

 

然后,我们还需要一个异常捕获类。继承 HandlerExceptionResolver
他的作用是发生的异常都会被他捕获。然后我们可以在他的重写方法中自定义了。无论是写日志还是返回数据。由你定
这个类是springMVC提供的。
我这里只是获取一下url路径,然后判断下异常的分类,然后记录下日志,返回数据。这里说明一下。
mv = new ModelAndView("jsonView", result.toMap()); 这个方法指的是后面的map类型返回给前台是json形式,这个jsonView是在spring-servlet.xml中配置的bean。
jsonView是这个json处理类的id名字,就是说让这个类处理map数据返回json。详情看我 这一篇博客

SpringExceptionResolver.java
package com.mmall.common;

import com.mmall.exception.ParamException;
import com.mmall.exception.PermissionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * Created by 敲代码的卡卡罗特
 * on 2018/3/24 22:26.
 */
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        String url = httpServletRequest.getRequestURL().toString();
        ModelAndView mv;
        String defaultMsg = "system erro";

        if (url.endsWith(".json")){
            if (e instanceof PermissionException || e instanceof ParamException){
                JsonData result = JsonData.fail(e.getMessage());
                mv = new ModelAndView("jsonView", result.toMap());
            }else {
                log.error("unknown json exception, url:" + url, e);
                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, e);
            JsonData result = JsonData.fail(defaultMsg);
            mv = new ModelAndView("exception", result.toMap());
        } else {
            log.error("unknow exception, url:" + url, e);
            JsonData result = JsonData.fail(defaultMsg);
            mv = new ModelAndView("jsonView", result.toMap());
        }

        return mv;
    }
}
View Code

 

 最后,我们需要在spring-servlet.xml中声明一下这个全局异常捕获类就可以了。就ok
 <bean class="com.mmall.common.SpringExceptionResolver" />

 


 



posted @ 2018-03-24 23:01  发疯的man  阅读(351)  评论(0编辑  收藏  举报