ResultDTO.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. *
  3. */
  4. package com.poyee.dto;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import lombok.experimental.Accessors;
  9. import java.io.Serializable;
  10. /**
  11. * 控制层统一返回数据格式
  12. *
  13. * @author poyee
  14. *
  15. */
  16. @Data
  17. @AllArgsConstructor
  18. @NoArgsConstructor
  19. @Accessors(chain = true)
  20. public class ResultDTO implements Serializable {
  21. /** 状态码 成功: 0 */
  22. public static final int RESPCODE_SUCCESS = 0;
  23. /** 状态码 失败: 1 */
  24. public static final int RESPCODE_FAILURE = 1;
  25. /** 权限不足 */
  26. public static final int RESPCODE_NO_AUTH = -1;
  27. /**返回成功信息*/
  28. public static final String MSG_SUCCESS = "OK";
  29. /** 失败信息FAIL */
  30. public static final String MSG_FAIL = "FAIL";
  31. /**权限不足返回信息*/
  32. public static final String MSG_NO_AUTH = "权限不足";
  33. /** 返回码 */
  34. private int code;
  35. /** 返回信息 */
  36. private String msg;
  37. /** 返回数据 */
  38. private Object data;
  39. /** 时间戳 */
  40. private Long timestamp=System.currentTimeMillis();
  41. public ResultDTO(int code, String msg, Object data) {
  42. this.code = code;
  43. this.msg = msg;
  44. this.data = data;
  45. }
  46. /**
  47. * 构造错误result
  48. *
  49. * @return
  50. */
  51. public static ResultDTO buildErrorResult() {
  52. return new ResultDTO(RESPCODE_FAILURE, MSG_FAIL, null);
  53. }
  54. /**
  55. * 构造空result
  56. *
  57. * @return 200 ok
  58. */
  59. public static ResultDTO buildEmptySuccess() {
  60. return new ResultDTO(RESPCODE_SUCCESS, MSG_SUCCESS, null);
  61. }
  62. /**
  63. * 构造错误result
  64. *
  65. * @return
  66. */
  67. public static ResultDTO buildErrorResult(String msg) {
  68. return new ResultDTO(RESPCODE_FAILURE, msg, null);
  69. }
  70. public static ResultDTO buildErrorResult(int code,String msg) {
  71. return new ResultDTO(code, msg, null);
  72. }
  73. public static ResultDTO build500Error(String msg) {
  74. return new ResultDTO(500, msg, null);
  75. }
  76. public static ResultDTO buildSuccessResult(Object o) {
  77. return new ResultDTO(RESPCODE_SUCCESS, MSG_SUCCESS, o);
  78. }
  79. }