全局统一返回实体
为了使在返回给浏览器数据时更加方便,适配各种返回情况,比如只返回成功或失败的信息、返回信息和数据,只返回数据等等。提高开发效率
全局统一返回实体的设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import lombok.Data; import lombok.experimental.Accessors;
import java.io.Serial; import java.io.Serializable;
@Data @Accessors(chain = true) public class Result<T> implements Serializable {
@Serial private static final long serialVersionUID = 5679018624309023727L;
public static final String SUCCESS_CODE = "0";
private String code;
private String message;
private T data;
private String requestId;
public boolean isSuccess() { return SUCCESS_CODE.equals(code); } }
|
@Accessors注解:位于Lombok插件包中,作用于类和属性上,对属性的getter和setter方法有影响。有三个属性:fluent、chain和prefix
fluent属性:默认为false,当该属性为true时,表示去除getter和setter方法的get和set,也就是说
1 2 3 4 5 6 7 8 9 10
| @Data @Accessors(fluent = false) public class Student{ private String name; public static void main(String args[]){ Student s = new Student(); s.setName("Tom"); String name = s.getName(); } }
|
1 2 3 4 5 6 7 8 9 10
| @Data @Accessors(fluent = true) public class Student{ private String name; public static void main(String args[]){ Student s = new Student(); s.name("Tom"); String name = s.name(); } }
|
chain属性:默认为false,当该属性为true时,调用bean的set方法可以返回该对象
1 2 3 4 5 6 7 8
| @Data @Accessors(chain = true) public class Student{ private String name; public static void main(String args[]){ Student s = new Student().setName("Tom"); } }
|
prefix属性:该属性是一个字符串数组,能够去掉getter和setter方法中属性指定的前缀
1 2 3 4 5 6 7 8 9 10 11
| @Data @Accessors(prefix = {"xx","yy"}) public class Student{ private String xxName; private String yyAge; public static void main(String args[]){ Student s = new Student(); s.setName("Tom"); String name = s.getName(); } }
|
全局统一返回实体构造器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import com.nageoffer.shortlink.admin.common.convention.errorcode.BaseErrorCode; import com.nageoffer.shortlink.admin.common.convention.exception.AbstractException;
import java.util.Optional;
public final class Results {
public static Result<Void> success() { return new Result<Void>() .setCode(Result.SUCCESS_CODE); }
public static <T> Result<T> success(T data) { return new Result<T>() .setCode(Result.SUCCESS_CODE) .setData(data); }
public static Result<Void> failure() { return new Result<Void>() .setCode(BaseErrorCode.SERVICE_ERROR.code()) .setMessage(BaseErrorCode.SERVICE_ERROR.message()); }
public static Result<Void> failure(AbstractException abstractException) { String errorCode = Optional.ofNullable(abstractException.getErrorCode()) .orElse(BaseErrorCode.SERVICE_ERROR.code()); String errorMessage = Optional.ofNullable(abstractException.getErrorMessage()) .orElse(BaseErrorCode.SERVICE_ERROR.message()); return new Result<Void>() .setCode(errorCode) .setMessage(errorMessage); }
public static Result<Void> failure(String errorCode, String errorMessage) { return new Result<Void>() .setCode(errorCode) .setMessage(errorMessage); } }
|
疑问:为啥要特意创建一个类来实例化另一个类?
解答:为了代码整洁?
通过本节需要掌握
- 全局统一返回实体的设计和使用
- 一些注解的使用