一、简单封装

一个简单的封装:

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. public class Result {
  8. private Object data;
  9. private Integer status;
  10. private String msg;
  11. public static Result ok(Object data){
  12. return new Result(data, 1, "数据请求成功");
  13. }
  14. public static Result error(String msg){
  15. return new Result("", 0, msg);
  16. }
  17. }

使用:

  1. Result.ok("ok");
  2. Result.error("error");

二、复杂一点的封装

定义一个错误码接口:

  1. public interface IErrorCode {
  2. long getCode();
  3. String getMessage();
  4. }

枚举一些常用API操作码:

  1. public enum ResultCode implements IErrorCode {
  2. SUCCESS(200, "操作成功"),
  3. FAILED(500, "操作失败"),
  4. VALIDATE_FAILED(404, "参数检验失败"),
  5. UNAUTHORIZED(401, "暂未登录或token已经过期"),
  6. FORBIDDEN(403, "没有相关权限");
  7. private long code;
  8. private String message;
  9. private ResultCode(long code, String message) {
  10. this.code = code;
  11. this.message = message;
  12. }
  13. public long getCode() {
  14. return code;
  15. }
  16. public String getMessage() {
  17. return message;
  18. }
  19. }

通用返回对象:

  1. @Data
  2. public class CommonResult<T> {
  3. private long code;
  4. private String message;
  5. private T data;
  6. protected CommonResult() {
  7. }
  8. protected CommonResult(long code, String message, T data) {
  9. this.code = code;
  10. this.message = message;
  11. this.data = data;
  12. }
  13. /**
  14. * 成功返回结果
  15. *
  16. * @param data 获取的数据
  17. */
  18. public static <T> CommonResult<T> success(T data) {
  19. return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
  20. }
  21. /**
  22. * 成功返回结果
  23. *
  24. * @param data 获取的数据
  25. * @param message 提示信息
  26. */
  27. public static <T> CommonResult<T> success(T data, String message) {
  28. return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
  29. }
  30. /**
  31. * 失败返回结果
  32. * @param errorCode 错误码
  33. */
  34. public static <T> CommonResult<T> failed(IErrorCode errorCode) {
  35. return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
  36. }
  37. /**
  38. * 失败返回结果
  39. * @param errorCode 错误码
  40. * @param message 错误信息
  41. */
  42. public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
  43. return new CommonResult<T>(errorCode.getCode(), message, null);
  44. }
  45. /**
  46. * 失败返回结果
  47. * @param message 提示信息
  48. */
  49. public static <T> CommonResult<T> failed(String message) {
  50. return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
  51. }
  52. /**
  53. * 失败返回结果
  54. */
  55. public static <T> CommonResult<T> failed() {
  56. return failed(ResultCode.FAILED);
  57. }
  58. /**
  59. * 参数验证失败返回结果
  60. */
  61. public static <T> CommonResult<T> validateFailed() {
  62. return failed(ResultCode.VALIDATE_FAILED);
  63. }
  64. /**
  65. * 参数验证失败返回结果
  66. * @param message 提示信息
  67. */
  68. public static <T> CommonResult<T> validateFailed(String message) {
  69. return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
  70. }
  71. /**
  72. * 未登录返回结果
  73. */
  74. public static <T> CommonResult<T> unauthorized(T data) {
  75. return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
  76. }
  77. /**
  78. * 未授权返回结果
  79. */
  80. public static <T> CommonResult<T> forbidden(T data) {
  81. return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
  82. }
  83. }

使用:

  1. CommonResult.success("Hello");
  2. CommonResult.failed("token已经过期");
  3. CommonResult.validateFailed("用户名或密码错误");
  4. CommonResult.unauthorized(null);