• 枚举类型 表示不同的 HTTP 状态码和相应的错误消息


    java web业务中经常用常量来表示不同的 HTTP 响应状态,比如

    public enum AppHttpCodeEnum {
    
        // 成功段0
        SUCCESS(200,"操作成功"),
        // 登录段1~50
        NEED_LOGIN(1,"需要登录后操作"),
        LOGIN_PASSWORD_ERROR(2,"密码错误"),
        // TOKEN50~100
        TOKEN_INVALID(50,"无效的TOKEN"),
        TOKEN_EXPIRE(51,"TOKEN已过期"),
        TOKEN_REQUIRE(52,"TOKEN是必须的"),
        // SIGN验签 100~120
        SIGN_INVALID(100,"无效的SIGN"),
        SIG_TIMEOUT(101,"SIGN已过期"),
        // 参数错误 500~1000
        PARAM_REQUIRE(500,"缺少参数"),
        PARAM_INVALID(501,"无效参数"),
        PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
        SERVER_ERROR(503,"服务器内部错误"),
        // 数据错误 1000~2000
        DATA_EXIST(1000,"数据已经存在"),
        AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"),
        DATA_NOT_EXIST(1002,"数据不存在"),
        // 数据错误 3000~3500
        NO_OPERATOR_AUTH(3000,"无权限操作"),
        NEED_ADMIND(3001,"需要管理员权限");
    
    
    
    
        int code;
        String errorMessage;
    
        AppHttpCodeEnum(int code, String errorMessage){
            this.code = code;
            this.errorMessage = errorMessage;
        }
    
        public int getCode() {
            return code;
        }
    
        public String getErrorMessage() {
            return errorMessage;
        }
    }
    
    • 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

    这种枚举的使用方式允许你在代码中使用这些常量来表示不同的 HTTP 响应状态,而不需要硬编码状态码和错误消息。这提高了代码的可读性和可维护性,并降低了错误的风险,因为你可以使用这些常量而不是手动输入状态码和消息。例如,你可以使用 AppHttpCodeEnum.SUCCESS.getCode() 来获取成功状态码,使用 AppHttpCodeEnum.SUCCESS.getErrorMessage() 来获取成功消息。并且无需要,实列化,直接写常量就会在加载服务的时候实列化,这个枚举类型 AppHttpCodeEnum 中定义的枚举常量是可以直接使用的,不需要创建新的枚举对象,就像使用静态变量一样

    一般配合响应类来进行使用

    public class ResponseResult<T> implements Serializable {
    
        private String host;
    
        private Integer code;
    
        private String errorMessage;
    
        private T data;
    
        public ResponseResult() {
            this.code = 200;
        }
    
        public ResponseResult(Integer code, T data) {
            this.code = code;
            this.data = data;
        }
    
        public ResponseResult(Integer code, String msg, T data) {
            this.code = code;
            this.errorMessage = msg;
            this.data = data;
        }
    
        public ResponseResult(Integer code, String msg) {
            this.code = code;
            this.errorMessage = msg;
        }
    
        public static ResponseResult errorResult(int code, String msg) {
            ResponseResult result = new ResponseResult();
            return result.error(code, msg);
        }
    
        public static ResponseResult okResult(int code, String msg) {
            ResponseResult result = new ResponseResult();
            return result.ok(code, null, msg);
        }
    
        public static ResponseResult okResult(Object data) {
            ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
            if(data!=null) {
                result.setData(data);
            }
            return result;
        }
    
        public static ResponseResult errorResult(AppHttpCodeEnum enums){
            return setAppHttpCodeEnum(enums,enums.getErrorMessage());
        }
    
        public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage){
            return setAppHttpCodeEnum(enums,errorMessage);
        }
    
        public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
            return okResult(enums.getCode(),enums.getErrorMessage());
        }
    
        private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage){
            return okResult(enums.getCode(),errorMessage);
        }
    
        public ResponseResult<?> error(Integer code, String msg) {
            this.code = code;
            this.errorMessage = msg;
            return this;
        }
    
        public ResponseResult<?> ok(Integer code, T data) {
            this.code = code;
            this.data = data;
            return this;
        }
    
        public ResponseResult<?> ok(Integer code, T data, String msg) {
            this.code = code;
            this.data = data;
            this.errorMessage = msg;
            return this;
        }
    
        public ResponseResult<?> ok(T data) {
            this.data = data;
            return this;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getErrorMessage() {
            return errorMessage;
        }
    
        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public String getHost() {
            return host;
        }
    
        public void setHost(String host) {
            this.host = host;
        }
    
    
    
    
    }
    
    
    • 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
  • 相关阅读:
    【车联网应用笔记】OTA从理论到落地
    湖仓一体技术解读|流式计算实现秒级数据入湖
    扫盲低代码——构建的基本原理
    Android13保存文件到sdcard报错:Operation not permitted
    测试工具之压测工具JMeter(一)
    iOS - 多线程-atomic
    Windows应急响应排查
    3D全景虚拟旅游在旅游行业中具备哪些应用价值?
    MySQL常用函数集锦 --- 字符串|数值|日期|流程函数总结
    深入linux内核架构--内存管理
  • 原文地址:https://blog.csdn.net/qq_55272229/article/details/134092901