ResultVo.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.java110.vo;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import java.io.Serializable;
  8. /**
  9. * @ClassName ResultVo
  10. * @Description TODO
  11. * @Author wuxw
  12. * @Date 2020/5/28 18:41
  13. * @Version 1.0
  14. * add by wuxw 2020/5/28
  15. **/
  16. public class ResultVo implements Serializable {
  17. public static final int CODE_ERROR = 404;// 未知异常
  18. public static final int CODE_OK = 0; // 成功
  19. public static final int CODE_MACHINE_OK = 0; // 成功
  20. public static final int CODE_MACHINE_ERROR = -1; // 未知异常
  21. public static final int CODE_UNAUTHORIZED = 401; //认证失败
  22. public static final int CODE_WECHAT_UNAUTHORIZED = 1401; //认证失败
  23. public static final int ORDER_ERROR = 500; //订单调度异常
  24. public static final String MSG_ERROR = "未知异常";// 未知异常
  25. public static final String MSG_OK = "成功"; // 成功
  26. public static final String MSG_UNAUTHORIZED = "认证失败"; //认证失败
  27. public static final int DEFAULT_RECORD = 1;
  28. public static final int DEFAULT_TOTAL = 1;
  29. // 分页页数
  30. private int page;
  31. // 行数
  32. private int rows;
  33. //页数
  34. private int records;
  35. // 总记录数
  36. private int total;
  37. //状态
  38. private int code;
  39. //错误提示
  40. private String msg;
  41. //数据对象
  42. private Object data;
  43. //用来存放大计、小计金额
  44. private Object sumTotal;
  45. public ResultVo() {
  46. }
  47. public ResultVo(int code, String msg) {
  48. this.code = code;
  49. this.msg = msg;
  50. }
  51. public ResultVo(Object data) {
  52. this.code = CODE_OK;
  53. this.msg = MSG_OK;
  54. this.data = data;
  55. }
  56. public ResultVo(int records, int total, Object data) {
  57. this.code = CODE_OK;
  58. this.msg = MSG_OK;
  59. this.records = records;
  60. this.total = total;
  61. this.data = data;
  62. }
  63. public ResultVo(int records, int total, Object data, Object sumTotal) {
  64. this.code = CODE_OK;
  65. this.msg = MSG_OK;
  66. this.records = records;
  67. this.total = total;
  68. this.data = data;
  69. this.sumTotal = sumTotal;
  70. }
  71. public ResultVo(int code, String msg, Object data) {
  72. this.code = code;
  73. this.msg = msg;
  74. this.data = data;
  75. }
  76. public ResultVo(int records, int total, int code, String msg, Object data) {
  77. this.records = records;
  78. this.total = total;
  79. this.code = code;
  80. this.msg = msg;
  81. this.data = data;
  82. }
  83. public int getPage() {
  84. return page;
  85. }
  86. public void setPage(int page) {
  87. this.page = page;
  88. }
  89. public int getRows() {
  90. return rows;
  91. }
  92. public void setRows(int rows) {
  93. this.rows = rows;
  94. }
  95. public int getRecords() {
  96. return records;
  97. }
  98. public void setRecords(int records) {
  99. this.records = records;
  100. }
  101. public int getTotal() {
  102. return total;
  103. }
  104. public void setTotal(int total) {
  105. this.total = total;
  106. }
  107. public int getCode() {
  108. return code;
  109. }
  110. public void setCode(int code) {
  111. this.code = code;
  112. }
  113. public String getMsg() {
  114. return msg;
  115. }
  116. public void setMsg(String msg) {
  117. this.msg = msg;
  118. }
  119. public Object getData() {
  120. return data;
  121. }
  122. public void setData(Object data) {
  123. this.data = data;
  124. }
  125. public Object getSumTotal() {
  126. return sumTotal;
  127. }
  128. public void setSumTotal(Object sumTotal) {
  129. this.sumTotal = sumTotal;
  130. }
  131. @Override
  132. public String toString() {
  133. return JSONObject.toJSONString(this, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
  134. }
  135. /**
  136. * 创建ResponseEntity对象
  137. *
  138. * @param data 数据对象
  139. * @return
  140. */
  141. public static ResponseEntity<String> createResponseEntity(Object data) {
  142. ResultVo resultVo = new ResultVo(DEFAULT_RECORD, DEFAULT_TOTAL, data);
  143. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  144. return responseEntity;
  145. }
  146. /**
  147. * 创建ResponseEntity对象
  148. *
  149. * @param resultVo 数据对象
  150. * @return
  151. */
  152. public static ResponseEntity<String> createResponseEntity(ResultVo resultVo) {
  153. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  154. return responseEntity;
  155. }
  156. /**
  157. * 成功通用回复
  158. *
  159. * @return
  160. */
  161. public static ResponseEntity<String> success() {
  162. ResultVo resultVo = new ResultVo(CODE_OK, MSG_OK);
  163. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  164. return responseEntity;
  165. }
  166. /**
  167. * 成功通用回复
  168. *
  169. * @return
  170. */
  171. public static ResponseEntity<String> error(String msg) {
  172. ResultVo resultVo = new ResultVo(CODE_ERROR, msg);
  173. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  174. return responseEntity;
  175. }
  176. /**
  177. * 创建ResponseEntity对象
  178. *
  179. * @param records 页数
  180. * @param total 总记录数
  181. * @param data 数据对象
  182. * @return
  183. */
  184. public static ResponseEntity<String> createResponseEntity(int records, int total, Object data) {
  185. ResultVo resultVo = new ResultVo(records, total, data);
  186. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  187. return responseEntity;
  188. }
  189. /**
  190. * 创建ResponseEntity对象
  191. *
  192. * @param records
  193. * @param total
  194. * @param data
  195. * @param sumTotal
  196. * @return
  197. */
  198. public static ResponseEntity<String> createResponseEntity(int records, int total, Object data, Object sumTotal) {
  199. ResultVo resultVo = new ResultVo(records, total, data, sumTotal);
  200. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  201. return responseEntity;
  202. }
  203. /**
  204. * 页面跳转
  205. *
  206. * @param url
  207. * @return
  208. */
  209. public static ResponseEntity<String> redirectPage(String url) {
  210. HttpHeaders headers = new HttpHeaders();
  211. headers.add(HttpHeaders.LOCATION, url);
  212. ResponseEntity<String> responseEntity = new ResponseEntity<String>("", headers, HttpStatus.FOUND);
  213. return responseEntity;
  214. }
  215. /**
  216. * 创建ResponseEntity对象
  217. *
  218. * @param code 状态嘛
  219. * @param msg 返回信息
  220. * @param data 数据对象
  221. * @return
  222. */
  223. public static ResponseEntity<String> createResponseEntity(int code, String msg, Object data) {
  224. ResultVo resultVo = new ResultVo(code, msg, data);
  225. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  226. return responseEntity;
  227. }
  228. /**
  229. * 创建ResponseEntity对象
  230. *
  231. * @param code 状态嘛
  232. * @param msg 返回信息
  233. * @return
  234. */
  235. public static ResponseEntity<String> createResponseEntity(int code, String msg) {
  236. ResultVo resultVo = new ResultVo(code, msg);
  237. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  238. return responseEntity;
  239. }
  240. /**
  241. * 创建ResponseEntity对象
  242. *
  243. * @param records 页数
  244. * @param total 总记录数
  245. * @param code 状态嘛
  246. * @param msg 返回信息
  247. * @param data 数据对象
  248. * @return
  249. */
  250. public static ResponseEntity<String> createResponseEntity(int records, int total, int code, String msg, Object data) {
  251. ResultVo resultVo = new ResultVo(records, total, code, msg, data);
  252. ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
  253. return responseEntity;
  254. }
  255. }