ResponseResult.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package space.anyi.BI.entity;
  2. import com.fasterxml.jackson.annotation.JsonInclude;
  3. import java.io.Serializable;
  4. /**
  5. * @Projectname: gditSpringBootLibrary
  6. * @Filename: ResponseResult
  7. * @Author: 杨逸
  8. * @Data:2023/12/14 17:48
  9. * @Description: 统一响应实体
  10. */
  11. @JsonInclude(JsonInclude.Include.NON_NULL)
  12. public class ResponseResult<T> implements Serializable {
  13. private Integer code;
  14. private String msg;
  15. private T data;
  16. public ResponseResult() {
  17. this.code = AppHttpCodeEnum.SUCCESS.getCode();
  18. this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
  19. }
  20. public ResponseResult(Integer code, T data) {
  21. this.code = code;
  22. this.data = data;
  23. }
  24. public ResponseResult(Integer code, String msg, T data) {
  25. this.code = code;
  26. this.msg = msg;
  27. this.data = data;
  28. }
  29. public ResponseResult(Integer code, String msg) {
  30. this.code = code;
  31. this.msg = msg;
  32. }
  33. public static ResponseResult errorResult(int code, String msg) {
  34. ResponseResult result = new ResponseResult();
  35. return result.error(code, msg);
  36. }
  37. public static ResponseResult okResult() {
  38. ResponseResult result = new ResponseResult();
  39. return result;
  40. }
  41. public static ResponseResult okResult(int code, String msg) {
  42. ResponseResult result = new ResponseResult();
  43. return result.ok(code, null, msg);
  44. }
  45. public static ResponseResult okResult(Object data) {
  46. ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
  47. if(data!=null) {
  48. result.setData(data);
  49. }
  50. return result;
  51. }
  52. public static ResponseResult errorResult(AppHttpCodeEnum enums){
  53. return setAppHttpCodeEnum(enums,enums.getMsg());
  54. }
  55. public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg){
  56. return setAppHttpCodeEnum(enums,msg);
  57. }
  58. public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
  59. return okResult(enums.getCode(),enums.getMsg());
  60. }
  61. private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){
  62. return okResult(enums.getCode(),msg);
  63. }
  64. public ResponseResult<?> error(Integer code, String msg) {
  65. this.code = code;
  66. this.msg = msg;
  67. return this;
  68. }
  69. public ResponseResult<?> ok(Integer code, T data) {
  70. this.code = code;
  71. this.data = data;
  72. return this;
  73. }
  74. public ResponseResult<?> ok(Integer code, T data, String msg) {
  75. this.code = code;
  76. this.data = data;
  77. this.msg = msg;
  78. return this;
  79. }
  80. public ResponseResult<?> ok(T data) {
  81. this.data = data;
  82. return this;
  83. }
  84. public Integer getCode() {
  85. return code;
  86. }
  87. public void setCode(Integer code) {
  88. this.code = code;
  89. }
  90. public String getMsg() {
  91. return msg;
  92. }
  93. public void setMsg(String msg) {
  94. this.msg = msg;
  95. }
  96. public T getData() {
  97. return data;
  98. }
  99. public void setData(T data) {
  100. this.data = data;
  101. }
  102. /**
  103. * 内部类,响应状态枚举
  104. */
  105. public enum AppHttpCodeEnum {
  106. // 成功
  107. SUCCESS(200,"操作成功"),
  108. // 登录
  109. NEED_LOGIN(401,"需要登录后操作"),
  110. NO_OPERATOR_AUTH(403,"无权限操作"),
  111. FILE_NOT_NULL(410,"文件不能为空"),
  112. FILE_TYPE_ERROR(411,"文件类型错误"),
  113. SYSTEM_ERROR(500,"出现错误"),
  114. USERNAME_EXIST(501,"用户名已存在"),
  115. REQUIRE_USERNAME(502, "必需填写用户名"),
  116. LOGIN_ERROR(503,"用户名或密码错误"),
  117. USER_NOT_NULL(510,"用户不能为空"),
  118. PASSWORD_NOT_NULL(511,"密码不能为空"),
  119. PASSWORD_ERROR(514, "密码错误"),
  120. USER_NOT_EXIST(515, "用户不存在"),
  121. PASSWORD_SAME(516, "新密码不能与旧密码一样"),
  122. VERIFY_CODE_ERROR(517, "验证码错误"),
  123. USER_STATE_ERROR(518, "用户状态值不正确"),
  124. FILE_ERROR(528, "文件读取错误"),
  125. FILE_IS_EMPTY_ERROR(529, "文件内容不能为空");
  126. private int code;
  127. private String msg;
  128. AppHttpCodeEnum(int code, String errorMessage){
  129. this.code = code;
  130. this.msg = errorMessage;
  131. }
  132. public int getCode() {
  133. return code;
  134. }
  135. public String getMsg() {
  136. return msg;
  137. }
  138. }
  139. }