通用相应类
This commit is contained in:
parent
baf51b2d5e
commit
45fa3c9202
|
|
@ -0,0 +1,26 @@
|
|||
package com.czg.resp;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author GYJoker
|
||||
*/
|
||||
@Getter
|
||||
public enum RespCode {
|
||||
SUCCESS(200, "操作成功"),
|
||||
FAILURE(500, "操作失败"),
|
||||
SYSTEM_ERROR(555, "系统内部错误"),
|
||||
RECORD_NOT_EXIST(601, "记录不存在"),
|
||||
RECORD_EXISTED(602, "记录已存在"),
|
||||
PARAM_ERROR(603, "参数错误"),
|
||||
UNAUTHORIZED(401, "未授权"),;
|
||||
|
||||
private final int code;
|
||||
private final String msg;
|
||||
|
||||
RespCode(int code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package com.czg.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Result类用于封装请求响应的结果,包含状态码、消息和数据
|
||||
* @author GYJ
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
private int code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
|
||||
// 私有构造函数,用于在类内部创建实例,强制通过静态方法来获取Result对象
|
||||
private Result(int code, String msg, T data) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
// 以下是三个静态方法,分别用于创建不同状态的Result对象
|
||||
|
||||
// 创建成功的响应结果对象,默认消息和无数据情况
|
||||
public static <T> Result<T> success() {
|
||||
return new Result<>(200, "操作成功", null);
|
||||
}
|
||||
|
||||
// 创建成功的响应结果对象,携带指定的数据
|
||||
public static <T> Result<T> success(T data) {
|
||||
return new Result<>(200, "操作成功", data);
|
||||
}
|
||||
|
||||
// 创建带有自定义消息的成功响应结果对象,携带指定的数据
|
||||
public static <T> Result<T> success(String msg, T data) {
|
||||
return new Result<>(200, msg, data);
|
||||
}
|
||||
|
||||
// 创建失败的响应结果对象,默认消息和无数据情况
|
||||
public static <T> Result<T> failure() {
|
||||
return new Result<>(500, "操作失败", null);
|
||||
}
|
||||
|
||||
// 创建失败的响应结果对象,携带指定的自定义消息
|
||||
public static <T> Result<T> failure(String msg) {
|
||||
return new Result<>(500, msg, null);
|
||||
}
|
||||
|
||||
// 创建带有自定义状态码和消息的失败响应结果对象,无数据情况
|
||||
public static <T> Result<T> failure(int code, String msg) {
|
||||
return new Result<>(code, msg, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(RespCode respCode) {
|
||||
return new Result<>(respCode.getCode(), respCode.getMsg(), null);
|
||||
}
|
||||
}
|
||||
|
|
@ -7,16 +7,11 @@
|
|||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.czg</groupId>
|
||||
<artifactId>cash-common</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>cash-common-dependencies</module>
|
||||
<!--
|
||||
<module>cash-common-log</module>
|
||||
-->
|
||||
<!---->
|
||||
<module>cash-common-tools</module>
|
||||
<module>cash-common-sa-token</module>
|
||||
</modules>
|
||||
|
|
|
|||
Loading…
Reference in New Issue