result結果返回類
import com.ljq.util.exception.ResultCode; import lombok.Data; /** * 結果返回類型 * @param <T> */ public class Result<T> { /** * 狀態碼值 */ 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 <T> Result<T> buildSuccessResult() { return new Result<T>(ResultCode.SUCCESS); } public static <T> Result<T> buildExceptionResult(String messages) { return new Result<T>(ResultCode.EXCEPTION, messages); } /** * 錯誤時返回的結果 * @param code 結果狀態碼 * @param message 錯誤消息 * @return */ @Deprecated public static <T> Result<T> buildFailuredResult(ResultCode code, String message) { return new Result<T>(code, message); } /** * 構建一個包含數據的成功結果 * * @param data * 數據 * @return */ public static <T> Result<T> buildSuccessResult(T data) { return new Result<T>(ResultCode.SUCCESS, null, data); } /** * 從目標jsonResult構建一個新的jsonResult不含data數據 * @param jsonResult * @return */ public static <T> Result<T> buildResultWithOutData(Result<?> jsonResult) { return new Result<T>(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<Object> serviceExceptionErrorHandler(HttpServletRequest req, HttpServletResponse response, ServiceException e) { int errorCode = e.getErrorCode(); log.error(String.valueOf(errorCode), e); response.setStatus(HttpStatus.NOT_EXTENDED.value()); return new Result<Object>(errorCode, e.getMessage(), null); } }
新建一個測試異常的枚舉
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]; } }
業務層調用測試