R.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.muzhi.huanshi.common;
  2. import io.swagger.annotations.ApiModelProperty;
  3. import lombok.Data;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. /**
  7. * @Author ZhangKan
  8. * @Data 11:20
  9. */
  10. //统一返回结果类
  11. @Data
  12. public class R {
  13. @ApiModelProperty(value = "是否成功")
  14. private Boolean success;
  15. @ApiModelProperty(value = "返回码")
  16. private Integer code;
  17. @ApiModelProperty(value = "返回消息")
  18. private String message;
  19. @ApiModelProperty(value = "返回数据")
  20. private Map<String, Object> data = new HashMap<String, Object>();
  21. //把构造方法私有
  22. private R(){}
  23. //成功静态方法
  24. public static R ok(){
  25. R r = new R();
  26. r.setSuccess(true);
  27. r.setCode(ResultCode.SUCCESS);
  28. r.setMessage("成功");
  29. return r;
  30. }
  31. //失败静态方法
  32. public static R error(){
  33. R r = new R();
  34. r.setSuccess(false);
  35. r.setCode(ResultCode.ERROR);
  36. r.setMessage("失败");
  37. return r;
  38. }
  39. public R message(String message){
  40. this.setMessage(message);
  41. return this;
  42. }
  43. public R code(Integer code){
  44. this.setCode(code);
  45. return this;
  46. }
  47. public R data(String key, Object value){
  48. this.data.put(key, value);
  49. return this;
  50. }
  51. public R data(String key, String value){
  52. this.data.put(key, value);
  53. return this;
  54. }
  55. public R data(Map<String, Object> map){
  56. this.setData(map);
  57. return this;
  58. }
  59. }