AjaxResult.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.example.demo.entity;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.commons.lang3.builder.ToStringBuilder;
  4. import org.apache.commons.lang3.builder.ToStringStyle;
  5. import java.util.HashMap;
  6. /**
  7. * 操作消息提醒
  8. *
  9. * @author zmd
  10. */
  11. public class AjaxResult extends HashMap<String, Object>
  12. {
  13. private static final long serialVersionUID = 1L;
  14. public static final String CODE_TAG = "code";
  15. public static final String MSG_TAG = "msg";
  16. public static final String DATA_TAG = "data";
  17. /**
  18. * 状态类型
  19. */
  20. public enum Type
  21. {
  22. /** 成功 */
  23. SUCCESS(0),
  24. /** 警告 */
  25. WARN(301),
  26. /** 错误 */
  27. ERROR(500);
  28. private final int value;
  29. Type(int value)
  30. {
  31. this.value = value;
  32. }
  33. public int value()
  34. {
  35. return this.value;
  36. }
  37. }
  38. /** 状态类型 */
  39. private Type type;
  40. /** 状态码 */
  41. private int code;
  42. /** 返回内容 */
  43. private String msg;
  44. /** 数据对象 */
  45. private Object data;
  46. /**
  47. * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
  48. */
  49. public AjaxResult()
  50. {
  51. }
  52. /**
  53. * 初始化一个新创建的 AjaxResult 对象
  54. *
  55. * @param type 状态类型
  56. * @param msg 返回内容
  57. */
  58. public AjaxResult(Type type, String msg)
  59. {
  60. super.put(CODE_TAG, type.value);
  61. super.put(MSG_TAG, msg);
  62. }
  63. /**
  64. * 初始化一个新创建的 AjaxResult 对象
  65. *
  66. * @param type 状态类型
  67. * @param msg 返回内容
  68. * @param data 数据对象
  69. */
  70. public AjaxResult(Type type, String msg, Object data)
  71. {
  72. super.put(CODE_TAG, type.value);
  73. super.put(MSG_TAG, msg);
  74. if (data!=null)
  75. {
  76. super.put(DATA_TAG, data);
  77. }
  78. }
  79. /**
  80. * 返回成功消息
  81. *
  82. * @return 成功消息
  83. */
  84. public static AjaxResult success()
  85. {
  86. return AjaxResult.success("操作成功");
  87. }
  88. /**
  89. * 返回成功数据
  90. *
  91. * @return 成功消息
  92. */
  93. public static AjaxResult success(Object data)
  94. {
  95. return AjaxResult.success("操作成功", data);
  96. }
  97. /**
  98. * 返回成功消息
  99. *
  100. * @param msg 返回内容
  101. * @return 成功消息
  102. */
  103. public static AjaxResult success(String msg)
  104. {
  105. return AjaxResult.success(msg, null);
  106. }
  107. /**
  108. * 返回成功消息
  109. *
  110. * @param msg 返回内容
  111. * @param data 数据对象
  112. * @return 成功消息
  113. */
  114. public static AjaxResult success(String msg, Object data)
  115. {
  116. return new AjaxResult(Type.SUCCESS, msg, data);
  117. }
  118. /**
  119. * 返回警告消息
  120. *
  121. * @param msg 返回内容
  122. * @return 警告消息
  123. */
  124. public static AjaxResult warn(String msg)
  125. {
  126. return AjaxResult.warn(msg, null);
  127. }
  128. /**
  129. * 返回警告消息
  130. *
  131. * @param msg 返回内容
  132. * @param data 数据对象
  133. * @return 警告消息
  134. */
  135. public static AjaxResult warn(String msg, Object data)
  136. {
  137. return new AjaxResult(Type.WARN, msg, data);
  138. }
  139. /**
  140. * 返回错误消息
  141. *
  142. * @return
  143. */
  144. public static AjaxResult error()
  145. {
  146. return AjaxResult.error("操作失败");
  147. }
  148. /**
  149. * 返回错误消息
  150. *
  151. * @param msg 返回内容
  152. * @return 警告消息
  153. */
  154. public static AjaxResult error(String msg)
  155. {
  156. return AjaxResult.error(msg, null);
  157. }
  158. /**
  159. * 返回错误消息
  160. *
  161. * @param msg 返回内容
  162. * @param data 数据对象
  163. * @return 警告消息
  164. */
  165. public static AjaxResult error(String msg, Object data)
  166. {
  167. return new AjaxResult(Type.ERROR, msg, data);
  168. }
  169. public Type getType()
  170. {
  171. return type;
  172. }
  173. public void setType(Type type)
  174. {
  175. this.type = type;
  176. }
  177. public int getCode()
  178. {
  179. return code;
  180. }
  181. public void setCode(int code)
  182. {
  183. this.code = code;
  184. }
  185. public String getMsg()
  186. {
  187. return msg;
  188. }
  189. public void setMsg(String msg)
  190. {
  191. this.msg = msg;
  192. }
  193. public Object getData()
  194. {
  195. return data;
  196. }
  197. public void setData(Object data)
  198. {
  199. this.data = data;
  200. }
  201. @Override
  202. public String toString()
  203. {
  204. return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("code", getCode())
  205. .append("msg", getMsg()).append("data", getData()).toString();
  206. }
  207. }