自定义异常类是为了在ajax增、删、改的时候返回异常参数给客户端,以便程序按照程序员的预期正常执行!
比如ajax请求修改在Controller中如此写:
/**
* 修改政策法规数据记录
*/
@RequestMapping(value = "/update")
@ResponseBody
public AjaxCommonObject update(@RequestBody CarPolicies dto) {
AjaxCommonObject ajaxCommonObject = new AjaxCommonObject();
try {
bizService.update(dto);
} catch (BizCommonException e) {
return new AjaxCommonObject(e);
}
return ajaxCommonObject;
}
其中AjaxCommonObject就是我们自定义的异常类:
public class AjaxCommonObject {
private int code;
private String message;
private Object data;
public AjaxCommonObject() {
this.code = 0;
this.message = "ajax业务请求正确响应";
}
public AjaxCommonObject(int code, String message) {
this.code = code;
this.message = message;
}
public AjaxCommonObject(BizCommonException bizCommonException) {
this.code = bizCommonException.getCode();
this.message = bizCommonException.getMessage();
}
public AjaxCommonObject(BizCommonExceptionEnum bizCommonExceptionEnum) {
this.code = bizCommonExceptionEnum.getCode();
this.message = bizCommonExceptionEnum.getMessage();
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return this.data;
}
public void setData(Object data) {
this.data = data;
}
}
其中在此类中有两个构造:
public AjaxCommonObject(BizCommonException bizCommonException) {
this.code = bizCommonException.getCode();
this.message = bizCommonException.getMessage();
}
public AjaxCommonObject(BizCommonExceptionEnum bizCommonExceptionEnum) {
this.code = bizCommonExceptionEnum.getCode();
this.message = bizCommonExceptionEnum.getMessage();
}
一、在BizCommonException类中要继承RuntimeException:
public class BizCommonException extends RuntimeException {
private int code;
private String message;
public BizCommonException(int code, String message) {
super(message);
this.code = code;
this.message = message;
}
public BizCommonException(BizCommonExceptionEnum bizCommonExceptionEnum) {
this.code = bizCommonExceptionEnum.getCode();
this.message = bizCommonExceptionEnum.getMessage();
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
}
二、BizCommonExceptionEnum 确定一些枚举类型,以便使用:
public enum BizCommonExceptionEnum {
DICT_EXISTED(400, "字典已经存在"),
DICT_NOT_EXISTED(400, "字典不存在"),
UPLOAD_ERROR(500, "上传图片出错"),
DB_RESOURCE_NULL(400, "数据库中没有该资源"),
NO_PERMITION(405, "权限异常"),
REQUEST_INVALIDATE(400, "请求数据格式不正确"),
INVALID_KAPTCHA(400, "验证码不正确"),
USER_ALREADY_REG(401, "该用户已经注册"),
NO_THIS_USER(400, "没有此用户"),
USER_NOT_EXISTED(400, "没有此用户"),
ROLE_NOT_EXISTED(400, "没有此角色"),
ACCOUNT_FREEZED(401, "账号被冻结"),
OLD_PWD_NOT_RIGHT(402, "原密码不正确"),
TWO_PWD_NOT_MATCH(405, "两次输入密码不一致"),
EXISTED_THE_MENU(400, "菜单编号重复,不能添加"),
EXISTED_THE_MENUNAME(400, "菜单名称重复"),
EXISTED_THE_ORGNAME(400, "组织名称重复"),
REQUEST_NULL(400, "请求数据格式错误,并且必传参数不能为空"),
SESSION_TIMEOUT(400, "会话超时"),
SERVER_ERROR(500, "服务器异常,请联系系统运维人员"),
EXISTED_THE_RECORD(406, "数据已经存在,不能重复操作"),
SAVE_DATA_ERROR(407, "数据保存失败,请联系运维人员或检查网络稍后再试!");
private int code;
private String message;
private BizCommonExceptionEnum(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
}