CommonResult.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.huimv.oauth2.api;
  2. /**
  3. * 通用返回对象
  4. * Created by macro on 2019/4/19.
  5. */
  6. public class CommonResult<T> {
  7. private long code;
  8. private String message;
  9. private T data;
  10. protected CommonResult() {
  11. }
  12. protected CommonResult(long code, String message, T data) {
  13. this.code = code;
  14. this.message = message;
  15. this.data = data;
  16. }
  17. /**
  18. * 成功返回结果
  19. *
  20. * @param data 获取的数据
  21. */
  22. public static <T> CommonResult<T> success(T data) {
  23. return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
  24. }
  25. /**
  26. * 成功返回结果
  27. *
  28. * @param data 获取的数据
  29. * @param message 提示信息
  30. */
  31. public static <T> CommonResult<T> success(T data, String message) {
  32. return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
  33. }
  34. /**
  35. * 失败返回结果
  36. * @param errorCode 错误码
  37. */
  38. public static <T> CommonResult<T> failed(IErrorCode errorCode) {
  39. return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
  40. }
  41. /**
  42. * 失败返回结果
  43. * @param errorCode 错误码
  44. * @param message 错误信息
  45. */
  46. public static <T> CommonResult<T> failed(IErrorCode errorCode,String message) {
  47. return new CommonResult<T>(errorCode.getCode(), message, null);
  48. }
  49. /**
  50. * 失败返回结果
  51. * @param message 提示信息
  52. */
  53. public static <T> CommonResult<T> failed(String message) {
  54. return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
  55. }
  56. /**
  57. * 失败返回结果
  58. */
  59. public static <T> CommonResult<T> failed() {
  60. return failed(ResultCode.FAILED);
  61. }
  62. /**
  63. * 参数验证失败返回结果
  64. */
  65. public static <T> CommonResult<T> validateFailed() {
  66. return failed(ResultCode.VALIDATE_FAILED);
  67. }
  68. /**
  69. * 参数验证失败返回结果
  70. * @param message 提示信息
  71. */
  72. public static <T> CommonResult<T> validateFailed(String message) {
  73. return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
  74. }
  75. /**
  76. * 未登录返回结果
  77. */
  78. public static <T> CommonResult<T> unauthorized(T data) {
  79. return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
  80. }
  81. /**
  82. * 未授权返回结果
  83. */
  84. public static <T> CommonResult<T> forbidden(T data) {
  85. return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
  86. }
  87. public long getCode() {
  88. return code;
  89. }
  90. public void setCode(long code) {
  91. this.code = code;
  92. }
  93. public String getMessage() {
  94. return message;
  95. }
  96. public void setMessage(String message) {
  97. this.message = message;
  98. }
  99. public T getData() {
  100. return data;
  101. }
  102. public void setData(T data) {
  103. this.data = data;
  104. }
  105. }