CommonResult.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.chuanghai.attendance.common.utils;
  2. import java.io.Serializable;
  3. /**
  4. * @Author: codingliang
  5. * @Description: 接口统一返回
  6. * @Date: 2021-04-29 12:10
  7. * @Version: V1.0
  8. **/
  9. public class CommonResult<T> extends BaseResult implements Serializable {
  10. private static final long serialVersionUID = 3616484754899974346L;
  11. public static CommonResult ok() {
  12. return ok("1", "执行成功");
  13. }
  14. public static <T> CommonResult<T> ok(String code, String msg) {
  15. return baseCreate(code, msg, true);
  16. }
  17. public static CommonResult fail() {
  18. return fail("-1", "执行失败");
  19. }
  20. public static CommonResult fail(String code, String msg) {
  21. return baseCreate(code, msg, false);
  22. }
  23. private static <T> CommonResult<T> baseCreate(String code, String msg, boolean success) {
  24. CommonResult result = new CommonResult();
  25. result.setCode(code);
  26. result.setSuccess(success);
  27. result.setMessage(msg);
  28. return result;
  29. }
  30. public CommonResult<T> setResult(T data) {
  31. this.setData(data);
  32. return this;
  33. }
  34. public T getData() {
  35. return (T) super.getData();
  36. }
  37. public static CommonResult result(String code, String msg, Boolean b) {
  38. return baseCreate(code, msg, b);
  39. }
  40. }