欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springmvc全局异常处理

程序员文章站 2022-04-21 23:48:41
...

idea使用springmvc的全局异常处理

  1. 首先需要在pom添加依赖

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>jsp-api</artifactId>
  <version>6.0.36</version>
</dependency>

     2、新建一个异常处理类,实现HandlerExceptionResolver接口并重写resolveException方法。代码如下

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

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

@Slf4j
@Component
public class SpringExceptionResolver implements HandlerExceptionResolver {

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

        return mv;
    }
}


  3、其中PermissionException是自己定义的异常

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


4、JsonData是返回给前端的Bean


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(){
        JsonData jsonData =new JsonData(true);
        return jsonData;
    }

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

5、其中jsonview是spring配置文件定义的一个bean

<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>