1. 创建HTTP状态码枚举 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 package com.example.demo.utils; import lombok.Getter; @Getter public enum HttpStatusEnum { /** * 操作成功 */ SUCCESS(200, "操作成功"), /** * 对象创建成功 */ CREATED(201, "对象创建成功"), /** * 请求已经被接受 */ ACCEPTED(202, "请求已经被接受"), /** * 操作已经执行成功,但是没有返回数据 */ NO_CONTENT(204, "操作已经执行成功,但是没有返回数据"), /** * 资源已被移除 */ MOVED_PERM(301, "资源已被移除"), /** * 重定向 */ SEE_OTHER(303, "重定向"), /** * 资源没有被修改 */ NOT_MODIFIED(304, "资源没有被修改"), /** * 参数列表错误(缺少,格式不匹配) */ BAD_REQUEST(400, "参数列表错误(缺少,格式不匹配)"), /** * 未授权 */ UNAUTHORIZED(401, "未授权"), /** * 访问受限,授权过期 */ FORBIDDEN(403, "访问受限,授权过期"), /** * 资源,服务未找到 */ NOT_FOUND(404, "资源,服务未找!"), /** * 不允许的http方法 */ BAD_METHOD(405, "不允许的http方法"), /** * 资源冲突,或者资源被锁 */ CONFLICT(409, "资源冲突,或者资源被锁"), /** * 不支持的数据,媒体类型 */ UNSUPPORTED_TYPE(415, "不支持的数据,媒体类型"), /** * 系统内部错误 */ ERROR(500, "系统内部错误"), /** * 接口未实现 */ NOT_IMPLEMENTED(501, "接口未实现"), /** * 系统警告消息 */ WARN(601,"系统警告消息"); private final Integer code; private final String message; HttpStatusEnum(Integer code, String message) { this.code = code; this.message = message; } } 2. 创建通用返回类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 package com.example.demo.utils; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class ResponseResult<T> { /*状态码*/ private Integer code; /*状态信息*/ private Boolean status; /*返回信息*/ private String message; /*返回数据*/ private T data; /** * 全参数方法 * * @param code 状态码 * @param status 状态 * @param message 返回信息 * @param data 返回数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ private static <T> ResponseResult<T> response(Integer code, Boolean status, String message, T data) { ResponseResult<T> responseResult = new ResponseResult<>(); responseResult.setCode(code); responseResult.setStatus(status); responseResult.setMessage(message); responseResult.setData(data); return responseResult; } /** * 全参数方法 * * @param code 状态码 * @param status 状态 * @param message 返回信息 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ private static <T> ResponseResult<T> response(Integer code, Boolean status, String message) { ResponseResult<T> responseResult = new ResponseResult<>(); responseResult.setCode(code); responseResult.setStatus(status); responseResult.setMessage(message); return responseResult; } /** * 成功返回(无参) * * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success() { return response(HttpStatusEnum.SUCCESS.getCode(), true, HttpStatusEnum.SUCCESS.getMessage(), null); } /** * 成功返回(枚举参数) * * @param httpResponseEnum 枚举参数 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success(HttpStatusEnum httpResponseEnum) { return response(httpResponseEnum.getCode(), true, httpResponseEnum.getMessage()); } /** * 成功返回(状态码+返回信息) * * @param code 状态码 * @param message 返回信息 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success(Integer code, String message) { return response(code, true, message); } /** * 成功返回(返回信息 + 数据) * * @param message 返回信息 * @param data 数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success(String message, T data) { return response(HttpStatusEnum.SUCCESS.getCode(), true, message, data); } /** * 成功返回(状态码+返回信息+数据) * * @param code 状态码 * @param message 返回信息 * @param data 数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success(Integer code, String message, T data) { return response(code, true, message, data); } /** * 成功返回(数据) * * @param data 数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success(T data) { return response(HttpStatusEnum.SUCCESS.getCode(), true, HttpStatusEnum.SUCCESS.getMessage(), data); } /** * 成功返回(返回信息) * * @param message 返回信息 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> success(String message) { return response(HttpStatusEnum.SUCCESS.getCode(), true, message, null); } /** * 失败返回(无参) * * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail() { return response(HttpStatusEnum.ERROR.getCode(), false, HttpStatusEnum.ERROR.getMessage(), null); } /** * 失败返回(枚举) * * @param httpResponseEnum 枚举 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail(HttpStatusEnum httpResponseEnum) { return response(httpResponseEnum.getCode(), false, httpResponseEnum.getMessage()); } /** * 失败返回(状态码+返回信息) * * @param code 状态码 * @param message 返回信息 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail(Integer code, String message) { return response(code, false, message); } /** * 失败返回(返回信息+数据) * * @param message 返回信息 * @param data 数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail(String message, T data) { return response(HttpStatusEnum.ERROR.getCode(), false, message, data); } /** * 失败返回(状态码+返回信息+数据) * * @param code 状态码 * @param message 返回消息 * @param data 数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail(Integer code, String message, T data) { return response(code, false, message, data); } /** * 失败返回(数据) * * @param data 数据 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail(T data) { return response(HttpStatusEnum.ERROR.getCode(), false, HttpStatusEnum.ERROR.getMessage(), data); } /** * 失败返回(返回信息) * * @param message 返回信息 * @param <T> 泛型 * @return {@link ResponseResult<T>} */ public static <T> ResponseResult<T> fail(String message) { return response(HttpStatusEnum.ERROR.getCode(), false, message, null); } } 3. 使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.example.demo.controller; import com.example.demo.utils.ResponseResult; @RequestMapping("") public class Hello { @GetMapping("/success") public ResponseResult<String> success() { return ResponseResult.success("操作成功"); } @GetMapping("/fail") public ResponseResult<String> fail() { return ResponseResult.fail(HttpStatusEnum.PARAM_ERROR); } }