封装统一返回数据格式

╰半夏微凉° 2023-10-03 18:11 112阅读 0赞
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. /**
  4. * 操作消息提醒
  5. *
  6. */
  7. public class AjaxResult extends HashMap<String, Object> {
  8. private static final long serialVersionUID = 1L;
  9. /**
  10. * 初始化一个新创建的 Message 对象
  11. */
  12. public AjaxResult() {
  13. }
  14. /**
  15. * 返回错误消息
  16. *
  17. * @return 错误消息
  18. */
  19. public static AjaxResult error() {
  20. return error(1, "操作失败");
  21. }
  22. /**
  23. * 返回错误消息
  24. *
  25. * @param msg 内容
  26. * @return 错误消息
  27. */
  28. public static AjaxResult error(String msg) {
  29. return error(500, msg);
  30. }
  31. /**
  32. * 返回错误消息
  33. *
  34. * @param code 错误码
  35. * @param msg 内容
  36. * @return 错误消息
  37. */
  38. public static AjaxResult error(int code, String msg) {
  39. AjaxResult json = new AjaxResult();
  40. json.put("code", code);
  41. json.put("msg", msg);
  42. return json;
  43. }
  44. /**
  45. * 返回错误消息
  46. *
  47. * @param code 错误码
  48. * @param msg 错误消息
  49. * @param data 附加内容
  50. * @return 错误消息
  51. */
  52. public static AjaxResult error(int code, String msg, Object data) {
  53. AjaxResult json = new AjaxResult();
  54. json.put("code", code);
  55. json.put("msg", msg);
  56. json.put("data", data);
  57. return json;
  58. }
  59. /**
  60. * 返回成功消息
  61. *
  62. * @param msg 内容
  63. * @return 成功消息
  64. */
  65. public static AjaxResult success(String msg) {
  66. AjaxResult json = new AjaxResult();
  67. json.put("msg", msg);
  68. json.put("code", 0);
  69. return json;
  70. }
  71. /**
  72. * 返回成功 带对象
  73. *
  74. * @param data 对象内容
  75. * @return 成功消息
  76. */
  77. public static AjaxResult success(Object data) {
  78. AjaxResult json = new AjaxResult();
  79. json.put("msg", "");
  80. json.put("code", 0);
  81. json.put("data", data);
  82. return json;
  83. }
  84. /**
  85. * 返回成功 带map
  86. *
  87. * @param map 对象内容
  88. * @return 成功消息
  89. */
  90. public static AjaxResult success(Map<String, Object> map) {
  91. AjaxResult json = new AjaxResult();
  92. json.put("msg", "");
  93. json.put("code", 0);
  94. json.putAll(map);
  95. return json;
  96. }
  97. /**
  98. * 返回成功 带对象
  99. *
  100. * @param data 对象内容
  101. * @return 成功消息
  102. */
  103. public static AjaxResult success(Object[] data) {
  104. AjaxResult json = new AjaxResult();
  105. json.put("msg", "");
  106. json.put("code", 0);
  107. json.put("data", data);
  108. return json;
  109. }
  110. /**
  111. * 返回成功消息
  112. *
  113. * @return 成功消息
  114. */
  115. public static AjaxResult success() {
  116. return AjaxResult.success("操作成功");
  117. }
  118. /**
  119. * 返回成功消息
  120. *
  121. * @param key 键值
  122. * @param value 内容
  123. * @return 成功消息
  124. */
  125. @Override
  126. public AjaxResult put(String key, Object value) {
  127. super.put(key, value);
  128. return this;
  129. }
  130. }

侵删

参考:

个人博客开发之blog-api项目统一结果集api封装 - 1024搜-程序员专属的搜索引擎

发表评论

表情:
评论列表 (有 0 条评论,112人围观)

还没有评论,来说两句吧...

相关阅读

    相关 统一json返回格式

    springmvc控制器接受到一个用户登录的请求, 要判断用户名密码是否为空, 还要去数据库检查密码是否正确。 然后以json的格式返回到前台, 前台再根据状态码来判断是允许登