top.isopen.commons.springboot.bean.Result Maven / Gradle / Ivy
package top.isopen.commons.springboot.bean;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 通用结果响应体
*
* 序列化依赖 getter 函数,不依赖构造函数
*
* 所以此处的注解只需使用 @Data
*
* 不需使用 @NoArgsConstructor 和 @AllArgsConstructor
*
* @author TimeChaser
* @version 1.0
* @since 2023/5/10 16:16
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class Result extends BaseResponse {
private static final long serialVersionUID = 2774312271443025267L;
private static final int SUCCESS_CODE = 0;
private int code;
private String message;
private String description;
private T data;
private static Builder builder() {
return new Builder<>();
}
public static Result> ok() {
return Result.builder()
.code(SUCCESS_CODE)
.build();
}
public static Result ok(T data) {
return Result.builder()
.code(SUCCESS_CODE)
.data(data)
.build();
}
public static Result ok(String message, String description, T data) {
return Result.builder()
.code(SUCCESS_CODE)
.message(message)
.description(description)
.data(data)
.build();
}
public static Result error(int code, String message, String description) {
return Result.builder()
.code(code)
.message(message)
.description(description)
.build();
}
private static class Builder {
private final Result result;
Builder() {
this.result = new Result<>();
}
private Result build() {
return result;
}
private Builder code(int code) {
result.code = code;
return this;
}
private Builder message(String message) {
result.message = message;
return this;
}
private Builder description(String description) {
result.description = description;
return this;
}
private Builder data(T data) {
result.data = data;
return this;
}
}
}