1. 返回结果实体类。

  1. /**
  2. * @Description 返回结果实体类
  3. * @Date 2022/2/15 16:24
  4. * @Author yxw
  5. */
  6. public class AjaxResult extends HashMap<String, Object> {
  7. private static final long serialVersionUID = 1L;
  8. /** 状态码 */
  9. private static final String CODE_TAG = "code";
  10. /** 返回内容 */
  11. private static final String MSG_TAG = "msg";
  12. /** 数据对象 */
  13. private static final String DATA_TAG = "data";
  14. /** 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 */
  15. public AjaxResult() {}
  16. /**
  17. * 初始化一个新创建的 AjaxResult 对象
  18. *
  19. * @param code 状态码
  20. * @param msg 返回内容
  21. */
  22. public AjaxResult(int code, String msg) {
  23. super.put(CODE_TAG, code);
  24. super.put(MSG_TAG, msg);
  25. }
  26. /**
  27. * 方便链式调用
  28. *
  29. * @param key
  30. * @param value
  31. * @return
  32. */
  33. @Override
  34. public Object put(String key, Object value) {
  35. super.put(key, value);
  36. return this;
  37. }
  38. /**
  39. * 初始化一个新创建的 AjaxResult 对象
  40. *
  41. * @param code 状态码
  42. * @param msg 返回内容
  43. * @param data 数据对象
  44. */
  45. public AjaxResult(int code, String msg, Object data) {
  46. super.put(CODE_TAG, code);
  47. super.put(MSG_TAG, msg);
  48. if (!Objects.isNull(data)) {
  49. super.put(DATA_TAG, data);
  50. }
  51. }
  52. /**
  53. * 返回成功消息
  54. *
  55. * @param msg 返回内容
  56. * @param data 数据对象
  57. * @return 成功消息
  58. */
  59. public static AjaxResult success(String msg, Object data) {
  60. return new AjaxResult(HttpStatus.SUCCESS, msg, data);
  61. }
  62. /**
  63. * 返回成功消息
  64. *
  65. * @param msg 返回内容
  66. * @return 成功消息
  67. */
  68. public static AjaxResult success(String msg) {
  69. return AjaxResult.success(msg, null);
  70. }
  71. /**
  72. * 返回成功数据
  73. *
  74. * @return 成功消息
  75. */
  76. public static AjaxResult success(Object data) {
  77. return AjaxResult.success("操作成功", data);
  78. }
  79. /**
  80. * 返回错误消息
  81. *
  82. * @param code 状态码
  83. * @param msg 返回内容
  84. * @return 警告消息
  85. */
  86. public static AjaxResult error(int code, String msg)
  87. {
  88. return new AjaxResult(code, msg, null);
  89. }
  90. /**
  91. * 返回错误消息
  92. *
  93. * @param msg 返回内容
  94. * @param data 数据对象
  95. * @return 警告消息
  96. */
  97. public static AjaxResult error(String msg, Object data)
  98. {
  99. return new AjaxResult(HttpStatus.ERROR, msg, data);
  100. }
  101. /**
  102. * 返回错误消息
  103. *
  104. * @return
  105. */
  106. public static AjaxResult error()
  107. {
  108. return AjaxResult.error("操作失败");
  109. }
  110. /**
  111. * 返回错误消息
  112. *
  113. * @param msg 返回内容
  114. * @return 警告消息
  115. */
  116. public static AjaxResult error(String msg)
  117. {
  118. return AjaxResult.error(msg, null);
  119. }
  120. }

2. 返回状态码实体类

  1. /**
  2. * @Description 返回状态码
  3. * @Date 2022/2/15 16:55
  4. * @Author yxw
  5. */
  6. public class HttpStatus {
  7. /**
  8. * 操作成功
  9. */
  10. public static final int SUCCESS = 200;
  11. /**
  12. * 对象创建成功
  13. */
  14. public static final int CREATED = 201;
  15. /**
  16. * 请求已经被接受
  17. */
  18. public static final int ACCEPTED = 202;
  19. /**
  20. * 操作已经执行成功,但是没有返回数据
  21. */
  22. public static final int NO_CONTENT = 204;
  23. /**
  24. * 资源已被移除
  25. */
  26. public static final int MOVED_PERM = 301;
  27. /**
  28. * 重定向
  29. */
  30. public static final int SEE_OTHER = 303;
  31. /**
  32. * 资源没有被修改
  33. */
  34. public static final int NOT_MODIFIED = 304;
  35. /**
  36. * 参数列表错误(缺少,格式不匹配)
  37. */
  38. public static final int BAD_REQUEST = 400;
  39. /**
  40. * 未授权
  41. */
  42. public static final int UNAUTHORIZED = 401;
  43. /**
  44. * 访问受限,授权过期
  45. */
  46. public static final int FORBIDDEN = 403;
  47. /**
  48. * 资源,服务未找到
  49. */
  50. public static final int NOT_FOUND = 404;
  51. /**
  52. * 不允许的http方法
  53. */
  54. public static final int BAD_METHOD = 405;
  55. /**
  56. * 资源冲突,或者资源被锁
  57. */
  58. public static final int CONFLICT = 409;
  59. /**
  60. * 不支持的数据,媒体类型
  61. */
  62. public static final int UNSUPPORTED_TYPE = 415;
  63. /**
  64. * 系统内部错误
  65. */
  66. public static final int ERROR = 500;
  67. /**
  68. * 接口未实现
  69. */
  70. public static final int NOT_IMPLEMENTED = 501;
  71. }