当前位置:网站首页>状态码封装--转载
状态码封装--转载
2022-04-21 19:30:00 【呆萌小新@渊洁】
文章目录
转载jeebase-WANGLEI并做补充
一. 常量封装
/** * @author jeebase-WANGLEI * @ClassName: PublicResultConstant * @Description: TODO * @date 2018年5月18日 下午11:49:45 */
public enum ResponseConstant {
/** * 成功 20x */
// 成功 服务器已经成功处理了请求。通常,这表示服务器提供了请求的网页。
SUCCESS_200(200, "success"),
// 已创建 请求成功并且服务器创建了新的资源。
SUCCESS_201(201, "success"),
//已接受 服务器已接受请求,但尚未处理。
SUCCESS_202(202, "尚未处理"),
// 重置内容 服务器成功处理了请求,但没有返回任何内容。
SUCCESS_205(205, "服务器成功处理了请求,但没有返回任何内容"),
/** * 异常 40x */
// 错误请求 服务器不理解请求的语法。
FAILED_400(400, "错误请求"),
// -未授权 请求要求身份验证。 token过期
UNAUTHORIZED_401(401, "未授权"),
// 禁止 服务器拒绝请求。
FAILED_403(403, "服务器拒绝请求"),
// 未找到 服务器到不到请求的网页。
FAILED_404(404, "服务器到不到请求的网页"),
// 408-请求超时 服务器等候请求时发生超时。
FAILED_408(408, "请求超时"),
/** * 服务器错误 50x */
// 服务器内部错误
SERVER_ERROR_500(500,"服务器内部错误"),
//服务器不可用 服务器目前无法使用(由于超载或者停机维护)。
SERVER_ERROR_503(503,"服务器不可用"),
/** * 操作失败 */
ERROR(90000000, "操作失败"),
/** * 参数错误 */
PARAM_ERROR(90000003, "参数错误"),
/** * 验证码错误 */
INVALID_RE_VCODE(10000011, "验证码错误"),
/** * 用户名或密码错误 */
INVALID_USERNAME_PASSWORD(10000003, "账号或密码错误"),
/** * */
INVALID_RE_PASSWORD(10000010, "两次输入密码不一致"),
/** * 用户名或密码错误 */
INVALID_PASSWORD(10000009, "旧密码错误"),
/** * 用户名重复 */
USERNAME_ALREADY_IN(10000002, "用户已存在"),
/** * 用户不存在 */
INVALID_USER(10000001, "用户不存在"),
/** * 角色不存在 */
INVALID_ROLE(10000004, "角色不存在"),
/** * 角色不存在 */
ROLE_USER_USED(10000008, "角色使用中,不可删除"),
/** * 参数错误-已存在 */
INVALID_PARAM_EXIST(10000005, "请求参数已存在"),
/** * 参数错误 */
INVALID_PARAM_EMPTY(10000006, "请求参数为空"),
/** * 没有权限 */
USER_NO_PERMITION(10000007, "当前用户无该接口权限");
public int result;
public String msg;
ResponseConstant(int result, String msg) {
this.result = result;
this.msg = msg;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
二. 返回状态码封装
转载Acerola;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/** * @author Acerola */
@Data
@AllArgsConstructor
public class Result<T> {
private int code;
private String msg;
private T data;
public Result() {
}
public Result(T data) {
this.data = data;
}
/** * 返回成功 */
public Result<T> success() {
return success("操作成功!");
}
/** * 返回成功 */
public Result<T> success(String message) {
return success(200, message);
}
/** * 文件上传成功 * * @param data * @param * @return */
public Result<T> fileSuccess(T data) {
Result<T> result = new Result<>(data);
result.setCode(0);
result.setMsg("成功");
return result;
}
/** * 返回成功 */
public Result<T> success(ResponseConstant constant) {
return success(constant.getResult(), constant.getMsg());
}
/** * 返回成功 */
public Result<T> success(int code, String message) {
this.setCode(code);
this.setMsg(message);
return this;
}
/** * 返回失败 */
public Result<T> error() {
return error("操作失败!");
}
/** * 返回失败 */
public Result<T> error(String messag) {
return error(500, messag);
}
/** * 返回失败 */
public Result<T> error(int code, String message) {
return success(code, message);
}
/** * 返回信息 */
public Result<T> error(ResponseConstant constant) {
return success(constant.getResult(), constant.getMsg());
}
/** * 放入object */
public Result<T> put(T object) {
this.setData(object);
return this;
}
}
版权声明
本文为[呆萌小新@渊洁]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_50913327/article/details/124281785
边栏推荐
- MySQL (III) index optimization and case analysis
- The fifth chapter uses Matplotlib to draw pie chart
- Pat (advanced level) 1096 - continuous
- 剑指Offer:[第29天 动态规划(困难)]--->n个骰子的点数
- 使用8266做串口调试工具二
- Delete Nan data in matrix or vector in MATLAB
- RobotFramework部分关键字无法使用或关键字为黑色
- Release announcement of HMS core version 6.4.0
- Flitter Xcode packaging and publishing failed error ninety thousand one hundred and sixty-five
- Introduction to GPS Beidou satellite time synchronization system (clock device) technology of power grid
猜你喜欢

Fundamentals of digital electronic technology 5.1 overview of trigger and 5.2 SR latch

switch分支

What are the factors affecting VPS website optimization?

Interviewer: a brief talk about go escape analysis?

Force buckle - 53 Maximum subarray sum

pfSense使用证书认证配置IPsec站点到站点隧道指南

Instruction of crystal Chem active GIP ELISA Kit

Seata(一) 服务配置以及启动

Rk3399—添加usb转串口驱动

2022.04.21 (lc_56_consolidation interval)
随机推荐
How to classify cosmetics in the management system?
在多文件中C语言中全局变量的重定义
Overview of relational database and non relational database, introduction to redis, common commands and optimization
Feign source code analysis
APM industry awareness series - eight - 25 advantages of Devops
学完这篇Charles抓包教程,我直接把fiddler卸载了
switch分支
C language quick sorting problem
Robot framework log output Chinese garbled code
Apache Doris creates dynamic partitions
Redis Lua script detailed explanation of lua script high frequency interview questions
Shandong University project training (IV) adding click events to multiple point markers
自动控制原理第6章——控制系统的校正及综合(思维导图)
APM industry awareness series - IX
调试 ms 源代码
leetcode18. 四数之和
Week to MySQL date
机器学习笔记 - 迹算子(跟踪运算符)
Questions fréquemment incorrectes pour la certification quotidienne du CISSP (20 avril 2022)
Analyzing the practical process of maker Education