Result.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * *************************************************
  3. * Copyright (c) 2018 QMTH. All Rights Reserved.
  4. * Created by Deason on 2018-07-16 15:00:21.
  5. * *************************************************
  6. */
  7. package cn.com.qmth.examcloud.app.model;
  8. import org.apache.commons.lang3.builder.ToStringBuilder;
  9. import org.apache.commons.lang3.builder.ToStringStyle;
  10. import java.io.Serializable;
  11. public class Result<T> implements Serializable {
  12. private static final long serialVersionUID = 1L;
  13. private String code;//状态码
  14. private String desc;//描述信息
  15. private T data;//数据
  16. public static String DESC_200 = "请求成功!";
  17. public static String DESC_500 = "请求失败!";
  18. public static String DESC_403 = "无效认证信息,请先登录!";
  19. public static String DESC_404 = "请求地址或参数错误!";
  20. public Result() {
  21. }
  22. public Result(String code, String desc, T data) {
  23. this.code = code;
  24. this.desc = desc;
  25. this.data = data;
  26. }
  27. /**
  28. * 成功结果
  29. */
  30. public Result success(T data) {
  31. this.code = Constants.CODE_200;
  32. this.desc = DESC_200;
  33. this.data = data;
  34. return this;
  35. }
  36. public Result success() {
  37. this.code = Constants.CODE_200;
  38. this.desc = DESC_200;
  39. return this;
  40. }
  41. /**
  42. * 错误结果
  43. */
  44. public Result error(String desc) {
  45. this.code = Constants.CODE_500;
  46. this.desc = desc;
  47. return this;
  48. }
  49. public Result error() {
  50. this.code = Constants.CODE_500;
  51. this.desc = DESC_500;
  52. return this;
  53. }
  54. public Result noAuthError() {
  55. this.code = Constants.CODE_403;
  56. this.desc = DESC_403;
  57. return this;
  58. }
  59. public Result noFoundError() {
  60. this.code = Constants.CODE_404;
  61. this.desc = DESC_404;
  62. return this;
  63. }
  64. public boolean isSuccess() {
  65. if (Constants.CODE_200.equals(getCode())) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. public String getCode() {
  71. return code;
  72. }
  73. public String getDesc() {
  74. return desc;
  75. }
  76. public T getData() {
  77. return data;
  78. }
  79. public void setCode(String code) {
  80. this.code = code;
  81. }
  82. public void setDesc(String desc) {
  83. this.desc = desc;
  84. }
  85. public void setData(T data) {
  86. this.data = data;
  87. }
  88. @Override
  89. public String toString() {
  90. return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
  91. }
  92. }