• 如何写出优雅的业务代码


    接口统一响应对象返回

    BaseResponse通用响应对象

    package com.leesin.project.common;存放在common包下

    有三个主要的字段:

    • code:一个整数,通常用于表示响应的状态码,例如200表示成功,404表示未找到资源等。
    • data:一个泛型类型T的对象,通常包含服务器返回的具体数据。
    • message:一个字符串,通常包含对响应的描述信息。

    提供了三个构造函数,允许创建不同类型的BaseResponse实例。

    1. BaseResponse(int code, T data, String message):创建一个新的响应实例,同时包含代码、数据和消息。
    2. BaseResponse(int code, T data):这是一个简化版本的构造函数,它将代码设置为输入参数,数据为null,消息为空字符串。
    3. BaseResponse(ErrorCode errorCode):创建一个新的响应实例,将代码设置为errorCode的code字段,数据设置为null,消息为errorCode的message字段。
    @Data
    public class BaseResponse<T> implements Serializable {
    
        private int code;
    
        private T data;
    
        private String message;
    
        public BaseResponse(int code, T data, String message) {
            this.code = code;
            this.data = data;
            this.message = message;
        }
    
        public BaseResponse(int code, T data) {
            this(code, data, "");
        }
    
        public BaseResponse(ErrorCode errorCode) {
            this(errorCode.getCode(), null, errorCode.getMessage());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ResultUtils工具类

    package com.leesin.project.common;存放在common包下

    是一个工具类,主要用于处理网络应用中的结果反馈。它提供了几个静态方法,这些方法返回一个BaseResponse对象,这个对象包含一个状态码(通常用于表示成功或失败),一个数据对象(如果有的话),以及一个消息。

    提供了以下功能:

    1. success(T data):这个方法创建一个新的BaseResponse对象,表示成功,并返回该对象。它接受一个类型为T的数据对象作为参数。
    2. error(ErrorCode errorCode):这个方法创建一个新的BaseResponse对象,表示出错,并返回该对象。它接受一个ErrorCode对象作为参数,这个对象通常包含错误代码和描述信息。
    3. error(int code, String message):这个方法创建一个新的BaseResponse对象,表示出错,并返回该对象。它接受一个错误代码和一个消息作为参数。
    4. error(ErrorCode errorCode, String message):这个方法类似于上面的方法,但是它接受一个额外的消息参数。
    public class ResultUtils {
    
        /**
         * 成功
         *
         * @param data
         * @param 
         * @return
         */
        public static <T> BaseResponse<T> success(T data) {
            return new BaseResponse<>(0, data, "ok");
        }
    
        /**
         * 失败
         *
         * @param errorCode
         * @return
         */
        public static BaseResponse error(ErrorCode errorCode) {
            return new BaseResponse<>(errorCode);
        }
    
        /**
         * 失败
         *
         * @param code
         * @param message
         * @return
         */
        public static BaseResponse error(int code, String message) {
            return new BaseResponse(code, null, message);
        }
    
        /**
         * 失败
         *
         * @param errorCode
         * @return
         */
        public static BaseResponse error(ErrorCode errorCode, String message) {
            return new BaseResponse(errorCode.getCode(), null, message);
        }
    }
    
    • 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

    ErrorCode自定义错误码

    package com.leesin.project.common;存放在common包下

    public enum ErrorCode {
    
        SUCCESS(0, "ok"),
        PARAMS_ERROR(40000, "请求参数错误"),
        NOT_LOGIN_ERROR(40100, "未登录"),
        NO_AUTH_ERROR(40101, "无权限"),
        NOT_FOUND_ERROR(40400, "请求数据不存在"),
        FORBIDDEN_ERROR(40300, "禁止访问"),
        SYSTEM_ERROR(50000, "系统内部异常"),
        OPERATION_ERROR(50001, "操作失败");
    
        /**
         * 状态码
         */
        private final int code;
    
        /**
         * 信息
         */
        private final String message;
    
        ErrorCode(int code, String message) {
            this.code = code;
            this.message = message;
        }
    
        public int getCode() {
            return code;
        }
    
        public String getMessage() {
            return message;
        }
    
    }
    
    • 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

    controller 层调用示例

    使用ResultUtils工具类

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    未待完续。。。

  • 相关阅读:
    【自动驾驶】路径规划—— Dubins 曲线公式总结及python代码实现(基于几何的方法)
    JVS应用中心
    手把手带你配置一个DHCP服务器
    Tomcat
    CleanMyMac X中文---一键优化Mac,释放存储空间新利器
    Allegro174版本如何关闭模块复用后铜皮自动从动态变成静态操作指导
    类和对象!
    TechSmith Camtasia2023屏幕录像和编辑软件更新介绍
    添加一个仅管理员可见的页面
    上周热点回顾(8.7-8.13)
  • 原文地址:https://blog.csdn.net/weixin_52767345/article/details/133933972