自定义通用返回对象

目的:给返回对象补充一些信息,告诉前端这个请求在业务层面上是成功还是失败,以及具体的描述信息。

我们需要自定义错误码(因为前端的HTTP状态码默认的值比较少)和正常错误返回类。

ErrorCode :

package com.heo.ezyuserbackend.common;

public enum ErrorCode {
   
    SUCCESS(0, "ok", ""),
    PARAMS_ERROR(40000, "请求参数错误", ""),
    NULL_ERROR(40001, "请求参数为空", ""),
    NOT_LOGIN(40100, "未登录", ""),
    NO_AUTH(40101, "无权限", ""),
    SYSTEM_ERROR(50000,"系统内部异常","");
    private final int code;

    /**
     * 状态码信息
     */
    private  final String message;

    public int getCode() {
   
        return code;
    }

    public String getMessage() {
   
        return message;
    }

    public String getDescription() {
   
        return description;
    }

    /**
     * 状态码描述(详情)
     */
    private final String description;

    ErrorCode(int code, String message, String description) {
   
        this.code = code;
        this.message = message;
        this.description = description;
    }
}

BaseResponse:

package com.heo.ezyuserbackend.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回类
 *
 * @param <T>
 */
@Data
public class BaseResponse<T> implements Serializable {
   

    private int code;

    private T data;

    private String message;

    private String description;


    public BaseResponse(int code, T data, String message, String description) {
   
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }

    public BaseResponse(int code, T data,String message) {
   
        this(code, data, message,"");
    }

    public BaseResponse(int code, T data) {
   
        this(code, data, "", "");
    }

    public BaseResponse(ErrorCode errorCode) {
   
        this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
    }
}

ResultUtil :

package com.heo.ezyuserbackend.common;

import com.fasterxml.jackson.databind.ser.Serializers;

/**
 * 返回工具类
 */
public class ResultUtil {
   
    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> success(T data) {
   
        return new BaseResponse<>(0, data, "ok");
    }

    /**
     * 失败
     *
     * @param errorCode
     * @return
     */
    public static <T> BaseResponse<T> error(ErrorCode errorCode) {
   
        return new BaseResponse<>(errorCode);
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @param description
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> error(int code, String message, String description) {
   
        return new BaseResponse<>(code, null, message, description);
    }

    /**
     * 失败
     *
     * @param errorCode
     * @param message
     * @param description
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> error(ErrorCode errorCode, String message, String description) {
   
        return new BaseResponse<>(errorCode.getCode(), null, message, description);
    }

    /**
     * 失败
     *
     * @param errorCode
     * @param description
     * @return
     */
    public static BaseResponse error(ErrorCode errorCode, String description) {
   
        return new BaseResponse<>(errorCode.getCode(), errorCode.getMessage(), description);
    }
}

相关推荐

  1. 定义通用返回对象

    2024-01-24 17:00:05       58 阅读
  2. SpringBoot 定义异常返回数据格式

    2024-01-24 17:00:05       26 阅读
  3. Spring Boot定义类处理API通用返回数据

    2024-01-24 17:00:05       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 17:00:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 17:00:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 17:00:05       82 阅读
  4. Python语言-面向对象

    2024-01-24 17:00:05       91 阅读

热门阅读

  1. 在直播软件中使用RTSP协议

    2024-01-24 17:00:05       47 阅读
  2. 深入解析MVCC:多版本并发控制的数据库之道

    2024-01-24 17:00:05       43 阅读
  3. 施工图纸上的常用符号

    2024-01-24 17:00:05       51 阅读
  4. linux批量新增用户、linux批量删除用户

    2024-01-24 17:00:05       52 阅读
  5. Android保存图片到系统图库并通知系统相册刷新

    2024-01-24 17:00:05       53 阅读
  6. k8s面试题

    2024-01-24 17:00:05       48 阅读
  7. Ubuntu20.04安装cuda12.11

    2024-01-24 17:00:05       67 阅读
  8. 给服务器开通telnet的流程

    2024-01-24 17:00:05       54 阅读
  9. Redis常见面试题

    2024-01-24 17:00:05       62 阅读
  10. docker与宿主机数据交换—ROS2

    2024-01-24 17:00:05       56 阅读
  11. k8s---HPA

    2024-01-24 17:00:05       53 阅读