mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-06-06 18:17:49 +08:00
全局统一返回对象实现
This commit is contained in:
22
src/main/java/cn/xf/basedemo/common/enums/SystemStatus.java
Normal file
22
src/main/java/cn/xf/basedemo/common/enums/SystemStatus.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package cn.xf.basedemo.common.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum SystemStatus {
|
||||
|
||||
SUSSES(200, "请求成功"),
|
||||
UNAVAILABILITY(401, "token无效"),
|
||||
ERROR(500, "系统异常")
|
||||
;
|
||||
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
SystemStatus(Integer code, String errorMessage) {
|
||||
this.code = code;
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,6 @@ public class LoginUser {
|
||||
private String account;
|
||||
|
||||
private String phone;
|
||||
|
||||
private String token;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xf.basedemo.common.model;
|
||||
|
||||
import cn.xf.basedemo.common.enums.SystemStatus;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -18,9 +19,47 @@ public class RetObj<T> {
|
||||
|
||||
private T data;
|
||||
|
||||
public RetObj(Integer code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public RetObj(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public RetObj(SystemStatus status) {
|
||||
this.code = status.getCode();
|
||||
this.message = status.getErrorMessage();
|
||||
}
|
||||
|
||||
public RetObj(SystemStatus status, T data) {
|
||||
this.code = status.getCode();
|
||||
this.message = status.getErrorMessage();
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public RetObj(Integer code, String errorMsg) {
|
||||
this.code = code;
|
||||
this.message = errorMsg;
|
||||
}
|
||||
|
||||
public static <T> RetObj<T> success() {
|
||||
return new RetObj();
|
||||
return new RetObj(SystemStatus.SUSSES);
|
||||
}
|
||||
|
||||
public static <T> RetObj<T> success(T data) {
|
||||
return new RetObj(SystemStatus.SUSSES, data);
|
||||
}
|
||||
|
||||
|
||||
public static <T> RetObj<T> error(SystemStatus status) {
|
||||
return new RetObj(status);
|
||||
}
|
||||
|
||||
public static <T> RetObj<T> error(String errorMsg) {
|
||||
return new RetObj(SystemStatus.ERROR.getCode(), errorMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user