一、定义请求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
@Setter
public 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<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;
}
}
三、定义自己的参数异常处理类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;
}
}