R.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.nokia.common;
  2. import lombok.Data;
  3. /**
  4. * 返回值的统一包装
  5. */
  6. @Data
  7. public class R {
  8. private Boolean success;
  9. private Integer code;
  10. private String message;
  11. private Object data = null;
  12. /**
  13. * 私有化构造方法,不允许在外部实例化
  14. */
  15. private R() {
  16. }
  17. /**
  18. * 成功的静态方法
  19. *
  20. * @return R实例
  21. */
  22. public static R ok() {
  23. R r = new R();
  24. r.setSuccess(true);
  25. r.setCode(1);
  26. r.setMessage("成功");
  27. return r;
  28. }
  29. /**
  30. * 失败的静态方法
  31. *
  32. * @return R实例
  33. */
  34. public static R error() {
  35. R r = new R();
  36. r.setSuccess(false);
  37. r.setCode(0);
  38. r.setMessage("失败");
  39. return r;
  40. }
  41. public R success(Boolean success) {
  42. this.setSuccess(success);
  43. return this;
  44. }
  45. public R code(Integer code) {
  46. this.setCode(code);
  47. return this;
  48. }
  49. public R data(Object object) {
  50. this.setData(object);
  51. return this;
  52. }
  53. public R message(String message) {
  54. this.setMessage(message);
  55. return this;
  56. }
  57. }