返回正确信息
创建一个工具类 CommonReturnType
//创建属性
//表明对应请求的返回处理结果 success或fail
private String status;
//若status=success 则data内返回前端需要的json数据
//若status=fail,则data内使用通用的错误码格式
private Object data;
//构造方法
//定义一个通用的创建方法
public static CommonReturnType create(Object result) {
return CommonReturnType.create(result,"success");
}
public static CommonReturnType create(Object result,String status) {
CommonReturnType type=new CommonReturnType();
type.setStatus(status);
type.setData(result);
return type;
}
返回错误信息
1.首先,需要创建一个接口
//获取枚举code值方法
public int getErrcode();
//获取枚举ErrMsg值方法
public String getErrMsg();
//通用错误类型 赋值错误信息errMsg方法
public CommonErroe setErrMsg(String errMsg);
2.创建一个枚举 实现上面接口 用来对应错误码和错误信息
public enum EmBusineError implements CommonErroe{
//定义枚举
//通用错误类型·00001
PARAMETER_VALIDATION_ERROR(00001,"参数不合法"),
//10000开头为用户信息相关错误定义
USER_NOT_EXIST(10001,"用户不存在")
;
private EmBusineError(int errCode,String errMsg){
this.errCode=errCode;
this.errMsg=errMsg;
}
private int errCode;
private String errMsg;
//实现获取枚举code值方法
@Override
public int getErrcode() {
return this.errCode;
}
//实现获取枚举ErrMsg值方法
@Override
public String getErrMsg() {
return this.errMsg;
}
//实现通用错误类型 修改错误信息errMsg方法
@Override
public CommonErroe setErrMsg(String errMsg) {
this.errMsg=errMsg;
return this;
}
}
3.包装器业务异常类实现
//包装器业务异常类实现
public class BusinessException extends Exception implements CommonErroe{
//枚举
private CommonErroe commonErroe;
//两个构造器 一个直接接收枚举 一个接收自定义errMsg的方式构造业务异常
//直接接收EmBusineError的传参用于构造业务的异常
public BusinessException(CommonErroe commonErroe){
super();
this.commonErroe=commonErroe;
}
//接收自定义errMsg的方式构造业务异常
public BusinessException(CommonErroe commonErroe,String errMsg){
super();
this.commonErroe=commonErroe;
this.commonErroe.setErrMsg(errMsg);
}
//调用枚举的实现获取枚举code值方法
@Override
public int getErrcode() {
return this.commonErroe.getErrcode();
}
//调用枚举实现获取枚举ErrMsg值方法
@Override
public String getErrMsg() {
return this.commonErroe.getErrMsg();
}
//调用枚举的实现通用错误类型 修改错误信息errMsg方法
@Override
public CommonErroe setErrMsg(String errMsg) {
return this.commonErroe.setErrMsg(errMsg);
}
}
使用
throw new BusinessException(EmBusineError.USER_NOT_EXIST);
或者通用自定义错误返回msg值
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"用户不存在");
异常处理
在controller定义
//定义exceptionhandler解决未被controller层吸收的exception
//添加三个注解
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request,Exception ex){
//返回前端异常处理信息
Map<String,Object> responserData =new HashMap<>();
//如果是自定义BusinessException异常执行这个
if (ex instanceof BusinessException){
//序号列化异常处理类
BusinessException businessException =(BusinessException)ex;
responserData.put("errCode",businessException.getErrcode());
responserData.put("errMsg",businessException.getErrMsg());
}else {
//如果不是自定义异常执行这个
responserData.put("errCode",EmBusineError.USER_ERROR.getErrcode());
responserData.put("errMsg",EmBusineError.USER_ERROR.getErrMsg());
}
//返回前端异常数据
return CommonReturnType.create(responserData,"fail");
}
优化 所有controller都可以用 全局异常处理
创建BaseController作为基类
public class BaseController {
//定义exceptionhandler解决未被controller层吸收的exception
//添加三个注解
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request, Exception ex){
//返回前端异常处理信息
Map<String,Object> responserData =new HashMap<>();
//如果是自定义BusinessException异常执行这个
if (ex instanceof BusinessException){
//序号列化异常处理类
BusinessException businessException =(BusinessException)ex;
responserData.put("errCode",businessException.getErrcode());
responserData.put("errMsg",businessException.getErrMsg());
}else {
//如果不是自定义异常执行这个
responserData.put("errCode", EmBusineError.USER_ERROR.getErrcode());
responserData.put("errMsg",EmBusineError.USER_ERROR.getErrMsg());
}
//返回前端异常数据
return CommonReturnType.create(responserData,"fail");
}
}
然后其他controller继承即可