1、响应体封装类

  1. package com.lms.demo.fileservice.util;
  2. import java.io.Serializable;
  3. /**
  4. * @Author: 李孟帅
  5. * @CreateTime: 2020-12-24 14:09
  6. * @Description: 响应结果封装
  7. */
  8. public class ResponseResult<T> implements Serializable {
  9. private static final long serialVersionUID = 4359709211352400087L;
  10. private final Integer status;
  11. private final String message;
  12. private final T data;
  13. public ResponseResult(int status, String message, T data) {
  14. this.status = status;
  15. this.message = message;
  16. this.data = data;
  17. }
  18. public static <T> ResponseResult<T> success() {
  19. return ResponseResult.result(StatusCode.S_200,StatusCode.S_200.message,null);
  20. }
  21. public static <T> ResponseResult<T> success(T data) {
  22. return ResponseResult.result(StatusCode.S_200,StatusCode.S_200.message,data);
  23. }
  24. public static <T> ResponseResult<T> success(String message, T data) {
  25. return ResponseResult.result(StatusCode.S_200,message,data);
  26. }
  27. public static <T> ResponseResult<T> error() {
  28. return ResponseResult.result(StatusCode.S_500,StatusCode.S_500.message,null);
  29. }
  30. public static <T> ResponseResult<T> error(T data) {
  31. return ResponseResult.result(StatusCode.S_500,StatusCode.S_500.message,data);
  32. }
  33. public static <T> ResponseResult<T> error(String message, T data) {
  34. return ResponseResult.result(StatusCode.S_500,message,data);
  35. }
  36. public static <T> ResponseResult<T> result(int status, String message) {
  37. return ResponseResult.result(status,message,null);
  38. }
  39. public static <T> ResponseResult<T> result(StatusCode statusCode, String message, T data) {
  40. return ResponseResult.result(statusCode.code, message, data);
  41. }
  42. public static <T> ResponseResult<T> result(int status, String message, T data) {
  43. return new ResponseResult<>(status, message, data);
  44. }
  45. public Integer getStatus() {
  46. return status;
  47. }
  48. public String getMessage() {
  49. return message;
  50. }
  51. public T getData() {
  52. return data;
  53. }
  54. @Override
  55. public String toString() {
  56. return "ResponseResult{" +
  57. "status=" + status +
  58. ", message='" + message + '\'' +
  59. ", data=" + data +
  60. '}';
  61. }
  62. public enum StatusCode {
  63. /**
  64. * @Author 李孟帅
  65. * @Date 2020/12/24 14:43
  66. * @Description 响应状态码
  67. */
  68. S_200("OK",200),
  69. S_201("Created",201),
  70. S_202("Accepted",202),
  71. S_204("Not Content",204),
  72. S_400("Bad Request",400),
  73. S_401("Unauthorized",401),
  74. S_403("Forbidden",403),
  75. S_404("Not Found",404),
  76. S_408("Request Timeout",408),
  77. S_409("Conflict",409),
  78. S_415("Unsupported Media Type",415),
  79. S_500("Internal Server Error",500),
  80. S_502("Bad Gateway",502);
  81. private final String message;
  82. private final int code;
  83. private StatusCode(String message,int code){
  84. this.message=message;
  85. this.code=code;
  86. }
  87. public String getMessage(){
  88. return message;
  89. }
  90. public int getCode() {
  91. return code;
  92. }
  93. }
  94. }