创建全局错误异常捕获
1、App.java
2、创建BaseController.java基类控制器
public class BaseController {
//跨域请求
public final static String CONTENT_TYPE="application/x-www-form-urlencoded";
//定义exceptionhandler解决未被controller层吸收的exception
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request,Exception ex){
Map<String,Object> responseData = new HashMap<>();
if (ex instanceof CommonException){
CommonException commonException = (CommonException)ex;
responseData.put("errorCode",commonException.getErrorCode());
responseData.put("errorMsg",commonException.getErrorMsg());
}else{
responseData.put("errorCode", EnumError.UNKNOW_ERROR.getErrorCode());
responseData.put("errorMsg",EnumError.UNKNOW_ERROR.getErrorMsg());
}
return CommonReturnType.create(responseData,"fail");
}
}
3、创建CommonError接口
//异常处理
public interface CommonError {
public int getErrorCode();
public String getErrorMsg();
public CommonError setErrormsg(String errorMsg);
}
4、创建CommonException.java
//包装器业务异常类实现
public class CommonException extends Exception implements CommonError{
private CommonError commonError;
public CommonException(CommonError commonError){
super();
this.commonError = commonError;
}
//接收自定义errorMsg的方式构造业务异常
public CommonException(CommonError commonError,String errorMsg){
super();
this.commonError = commonError;
this.commonError.setErrormsg(errorMsg);
}
@Override
public int getErrorCode() {
return this.commonError.getErrorCode();
}
@Override
public String getErrorMsg() {
return this.commonError.getErrorMsg();
}
@Override
public CommonError setErrormsg(String errorMsg) {
this.commonError.setErrormsg(errorMsg);
return this;
}
}
5、创建枚举EnumError.java
public enum EnumError implements CommonError{
//未知错误
UNKNOW_ERROR(99998,"未知错误"),
//定义一个通用类型的错误
COMMON_ERROR(99999,"参数不合法"),
//验证码输入错误,匹配不成功
VCODE_ERROR(99997,"验证码输入有误,请重新输入"),
//参数不能为空
PRAMA_NOT_EMPTY_ERROR(99996,"参数不能为空"),
//10000开头代表用户相关信息错误定义
USER_NOT_EXIST(10001,"用户信息不存在"),
;
private int errorCode;
private String errorMsg;
//构造函数
private EnumError(int errorCode,String errorMsg){
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
@Override
public int getErrorCode() {
return this.errorCode;
}
@Override
public String getErrorMsg() {
return this.errorMsg;
}
@Override
public CommonError setErrormsg(String errorMsg) {
this.errorMsg=errorMsg;
return this;
}
}
6、创建CommonReturnType.java
public class CommonReturnType {
//返回对应请求的处理处理状态
private String status;
//返回请求的json数据
private Object data;
//定义一个通用的创建方法、
public static CommonReturnType create(Object result){
return CommonReturnType.create(result,"success");
}
public static CommonReturnType create(Object result,String status){
CommonReturnType data = new CommonReturnType();
data.setStatus(status);
data.setData(result);
return data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}