1. 定義接口統一返回標準java
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Getter; import lombok.Setter; import java.io.Serializable; @ApiModel(value = "統一返回數據格式") @Getter @Setter public class ResultMsg implements Serializable { @ApiModelProperty(value = "狀態碼") private int code; @ApiModelProperty(value = "返回結果描述") private String msg; @ApiModelProperty(value = "返回結果") private Object data; public ResultMsg(ResultMsgStatus resultMsgStatus) { this.code = resultMsgStatus.getCode(); this.msg = resultMsgStatus.getMsg(); } public ResultMsg(ResultMsgStatus resultMsgStatus, Object data) { this.code = resultMsgStatus.getCode(); this.msg = resultMsgStatus.getMsg(); this.data = data; } private ResultMsg setMsg(String msg) { this.msg = msg; return this; } public static ResultMsg success(Object data) { return new ResultMsg(ResultMsgStatus.SUCCESS, data); } public static ResultMsg success() { return new ResultMsg(ResultMsgStatus.SUCCESS, ""); } public static ResultMsg failure(String failMsg) { return new ResultMsg(ResultMsgStatus.FAILURE).setMsg(failMsg); } }
2. 定義錯誤碼web
import lombok.Getter; @Getter public enum ResultMsgStatus { /** * 請求失敗 */ FAILURE(0, "請求失敗"), /** * 請求成功 */ SUCCESS(200, "成功"), /** * Token無效 */ TOKEN_INVALID(401, "Token無效"), /** * 服務器錯誤 */ SERVER_ERROR(500, "服務器錯誤"); private int code; private String msg; private ResultMsgStatus(int code, String msg) { this.code = code; this.msg = msg; } }
3. 定義異常基類spring
import com.bubble.salt.msg.ResultMsgStatus; /** * 異常基類,全部自定義的異常類均須要派生自該類 */ public class BaseException extends RuntimeException{ /** * 具體異常碼 */ private int code=0; public BaseException(ResultMsgStatus status, Throwable cause) { super(status.getMsg(), cause); this.code=status.getCode(); } public BaseException(ResultMsgStatus status) { super(status.getMsg()); this.code=code; } public BaseException(String msg){ super(msg); this.code = ResultMsgStatus.FAILURE.getCode(); } public BaseException(ResultMsgStatus status, String msg){ super(msg); this.code = status.getCode(); } public int getCode() { return code; } protected void setCode(int code){ this.code=code; } }
4. 自定義異常服務器
import com.bubble.salt.msg.ResultMsgStatus; /** * @BelongsPackage: com.bubble.salt.exception * @CreateTime: 2019-09-19 16:24 * @Description: 用戶異常 */ public class UserException extends BaseException { public UserException(ResultMsgStatus status, Throwable cause) { super(status, cause); } public UserException(ResultMsgStatus status) { super(status); } public UserException(String msg) { super(msg); } public UserException(ResultMsgStatus status, String msg) { super(status, msg); } }
5. 全局異常處理this
import com.bubble.salt.msg.ResultMsg; import com.bubble.salt.msg.ResultMsgStatus; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) @ResponseBody public ResultMsg globalException(Exception e) { log.warn(e.getMessage(), e); //更具本身定義的異常進行自本身須要的處理 if (e instanceof UserException) { return new ResultMsg(ResultMsgStatus.TOKEN_INVALID, e.getMessage()); } return ResultMsg.failure("服務器開小差了!"); } }