result结果返回类
import com.ljq.util.exception.ResultCode; import lombok.Data; /** * 结果返回类型 * @param*/ public class Result { /** * 状态码值 */ private int code; /** * 结果说明 */ private String message; /** * 结果值 */ private T data; public Result() { this.setCode(ResultCode.SUCCESS); this.setMessage(null); } public Result(ResultCode code) { this.setCode(code); // 成功的时候不填充默认信息 if (ResultCode.SUCCESS != code) { this.setMessage(code.getErrorMessage()); } } public Result(ResultCode code, String message) { this.setCode(code); this.setMessage(message); } public Result(ResultCode code, String message, T data) { this.setCode(code); this.setMessage(message); this.setData(data); } public Result(int codeVal, String message, T data) { this.setCode(codeVal); this.setMessage(message); this.setData(data); } public Result(ResultCode success, T data) { this.setCode(success); this.setData(data); } public int getCode() { return code; } public void setCode(int codeVal) { this.code = codeVal; } public void setCode(ResultCode code) { this.code = code.getErrorCode(); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } /** * 构建一个成功状态的结果 * * @return */ public static Result buildSuccessResult() { return new Result (ResultCode.SUCCESS); } public static Result buildExceptionResult(String messages) { return new Result (ResultCode.EXCEPTION, messages); } /** * 错误时返回的结果 * @param code 结果状态码 * @param message 错误消息 * @return */ @Deprecated public static Result buildFailuredResult(ResultCode code, String message) { return new Result (code, message); } /** * 构建一个包含数据的成功结果 * * @param data * 数据 * @return */ public static Result buildSuccessResult(T data) { return new Result (ResultCode.SUCCESS, null, data); } /** * 从目标jsonResult构建一个新的jsonResult不含data数据 * @param jsonResult * @return */ public static Result buildResultWithOutData(Result> jsonResult) { return new Result (jsonResult.getCode(), jsonResult.getMessage(), null); } }
新建一个ErrorCode接口
package com.ljq.util.exception; /** * 错误码 * @author ljq * */ public interface ErrorCode { /** * 错误码的最大长度 */ public final static int ERROR_CODE_LENGTH = 10000; /** * 获取错误码 * @return 具体错误码 */ int getErrorCode(); /** * 获取错误信息 * @return 具体错误码对应信息 */ String getErrorMessage(); /** * 获取错误信息占位符数组 * @return */ Object[] getMessageArgs(); }
新建一个applicationException异常类继承RuntimeException
package com.ljq.util.exception; /** * 应用系统异常类 */ public class ApplictionException extends RuntimeException { private static final long serialVersionUID = -1L; /** * 应用错误码 */ protected int errorCode; /** * 异常级别,默认为错误 */ /** * 应用异常 */ public ApplictionException() { super(); } /** * 应用异常 * @param code 异常码 */ public ApplictionException(ErrorCode code){ super(code.getErrorMessage()); this.errorCode = code.getErrorCode(); } /** * 应用异常 * @param code * @param e */ public ApplictionException(ErrorCode code,Throwable e){ super(code.getErrorMessage(),e); this.errorCode = code.getErrorCode(); } public ApplictionException(int errorCode,Throwable e){ super(e); this.errorCode = errorCode; } public ApplictionException(int errorCode){ super(); this.errorCode = errorCode; } /** * 异常错误 * @param errorCode * @param message */ public ApplictionException(int errorCode,String message){ super(message); this.errorCode = errorCode; } /** * @param message * @param cause * @param enableSuppression * @param writableStackTrace */ public ApplictionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } /** * @param message * @param cause */ public ApplictionException(String message, Throwable cause) { super(message, cause); } /** * @param message */ public ApplictionException(String message) { super(message); } /** * @param cause */ public ApplictionException(Throwable cause) { super(cause); } /** * 获取错误码 * @return the errorCode */ public int getErrorCode() { return errorCode; } /** * 设置错误码 * @param code the code to set */ public void setErrorCode(int code) { this.errorCode = code; } }
新建一个业务层异常类ServiceException
/** * 业务层异常类 * */ public class ServiceException extends ApplictionException { private static final long serialVersionUID = -1L; /** * 业务层异常 */ public ServiceException(ErrorCode error){ super(error); } /** * 业务层异常 * @param code 错误码 * @param e */ public ServiceException(ErrorCode code,Throwable e){ super(code,e); } /** * 业务层异常 * @param errorCode 错误码 */ public ServiceException(int errorCode,Throwable e){ super(errorCode,e); } /** * 业务层异常 * @param errorCode 错误码 */ public ServiceException(int errorCode,String message){ super(errorCode,message); } /** * 业务层异常 * @param errorCode 错误码 */ public ServiceException(int errorCode){ super(errorCode); } }
新建异常拦截类BaseExceptionHandler
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ljq.util.vo.Result; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; import lombok.extern.slf4j.Slf4j; /** * 处理异常类 */ @Slf4j @RestControllerAdvice @ResponseBody public class BaseExceptionHandler { @Value("${application.code:0}") private int applicationCode; /** * 业务层异常统一解析 * @param req * @param response * @param e * @return */ @ExceptionHandler(value = ServiceException.class) public Result
新建一个测试异常的枚举
import com.ljq.util.exception.ErrorCode; import lombok.Data; /** * 统一异常枚举 */ public enum ApplicationEnum implements ErrorCode { createException(101, "新增失败"), updateException(101, "更新失败"), deleteException(101, "删除失败"); private Integer errorCode; private String errorMessage; public void setErrorCode(Integer errorCode) { this.errorCode = errorCode; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } ApplicationEnum(Integer errorCode, String errorMessage) { this.errorCode = errorCode; this.errorMessage = errorMessage; } @Override public int getErrorCode() { return this.errorCode; } @Override public String getErrorMessage() { return this.errorMessage; } @Override public Object[] getMessageArgs() { return new Object[0]; } }
业务层调用测试