1. 自定义业务异常类
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = 1L;
private ResultCode resultCode;
public ServiceException(ResultCode resultCode) {
this(resultCode.message());
this.resultCode = resultCode;
}
private ServiceException(String message) {
super(message);
}
public ResultCode getResultCode() {
return resultCode;
}
public void setResultCode(ResultCode resultCode) {
this.resultCode = resultCode;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
2. 创建统一的返回体
package cn.com.wind.sprtes.exception;
import java.io.Serializable;
public class Result implements Serializable {
private static final long serialVersionUID = 1L;
private Integer code;
private String msg;
private Object data;
private Result() {
}
private Result(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
private void setResultCode(ResultCode code) {
this.code = code.code();
this.msg = code.message();
}
public static Result failure(Integer code, String msg) {
Result result = new Result(code, msg);
return result;
}
public static Result success() {
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
return result;
}
public static Result success(Object data) {
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
result.setData(data);
return result;
}
public static Result failure(ResultCode resultCode) {
Result result = new Result();
result.setResultCode(resultCode);
return result;
}
public static Result failure(ResultCode resultCode, Object data) {
Result result = new Result();
result.setResultCode(resultCode);
result.setData(data);
return result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
3. 枚举 创建统一异常码
public enum ResultCode {
SUCCESS(1, "操作成功"),
ERROR(2, "操作失败,未知制定错误信息"),
PARAM_IS_INVALID(10001, "参数无效"),
PARAM_IS_BLANK(10002, "参数为空"),
PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
PARAM_NOT_COMPLETE(10004, "参数缺失"),
USER_NOT_LOGIN(20001, "用户未登录"),
USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
USER_NOT_EXIST(20004, "用户不存在"),
USER_HAS_EXISTED(20005, "用户已存在"),
USER_NOT_EMPTY(20006, "用户名不能为空"),
USER_MUST_BE_IS_NUMBER_AND_LETTER(20007, "密码只能为数字或者字母"),
USER_BACTH_DELETE_ERROR(20008, "批量删除报错"),
SPECIFIED_QUESTIONED_USER_NOT_EXIST(30001, "业务逻辑出现问题"),
SYSTEM_INNER_ERROR(40001, "系统内部错误,请稍后重试"),
DATA_NONE(50001, "数据未找到"),
DATA_WRONG(50002, "数据错误"),
DATA_EXISTED(50003, "数据已存在"),
INTERFACE_INNER_INVOKE_ERROR(60001, "内部系统接口调用异常"),
INTERFACE_OUTTER_INVOKE_ERROR(60002, "外部系统接口调用异常"),
INTERFACE_FORBID_VISIT(60003, "该接口禁止访问"),
INTERFACE_ADDRESS_INVALID(60004, "接口地址无效"),
INTERFACE_REQUEST_TIMEOUT(60005, "接口请求超时"),
PERMISSION_NO_ACCESS(70001, "无访问权限"),
ROLE_IS_EXIST(80001, "角色不存在"),
ROLE_MSG_NOT_AGREEMENT(80002, "角色信息不一致");
private Integer code;
private String message;
ResultCode(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer code() {
return this.code;
}
public String message() {
return this.message;
}
}
4. 创建全局异常处理类
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(Exception.class)
public Object handleException(Exception e) {
StackTraceElement[] stackTrace = e.getStackTrace();
Result result = null;
if (e instanceof ServiceException) {
result = Result.failure(((ServiceException) e).getResultCode());
} else {
result = Result.failure(500, "服务器意外错误:" + e.getMessage());
}
return result;
}
}