后端返回值方法

后端接口怎么将值返回给前端呢?以下是其中一种方法:

1、返回实体类

@Data
@Builder
public class ResEntity implements Serializable {
    /**
     * 错误编号
     */
    private Integer code;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 返回对象
     */
    private Object data;
    /**
     * 是否成功
     */
    private boolean success;
}

2、返回状态码


import lombok.Getter;

/**
 * 错误码枚举类
 *
 * @author ZhengGong
 * @date 2019/6/12
 */
@Getter
public enum ResCode {

    SUCCESS(0, "成功!"),
    OK(200, "OK"),
    ERROR(201, "失败"),
    REQUEST_NOT_FOUND(202, "请求不存在!"),
    HTTP_BAD_METHOD(203, "请求方式不支持!"),
    BAD_REQUEST(204, "请求异常!"),
    PARAM_NOT_MATCH(205, "参数不匹配!"),
    PARAM_NOT_NULL(206, "参数不能为空!"),
    JSON_PARSE_ERROR(207, "JSON转换异常"),
    C8_UNSUPPORTED_ENCODING_ERROR(208, "双编码异常"),
    AUTHORIZE_ERROR(209, "请求未授权!"),
    AUTHORIZE_UP_ERROR(210, "请求未授权,账号或密码错误!"),


    /**
     * 业务类
     */
    DMP_DATA_NULL(3001, "找不到对应的数据,或者数据已经同步成功"),
    DMP_DATA_TRANSLATE_FAIL(3002, "调用DMP同步数据失败,请重试"),
    DMP_FILE_URL_ILLEGAL(3003, "获取到的DMP文件路径不合法"),
    DMP_FILE_PUBLISH_ERROR(3004, "DMP发布文件失败"),
    SKU_CODE_NOTFOUND(3005, "SKU CODE没有找到相关的SKU数据"),
    SKU_CODE_DUPLICATE(3006, "SKU CODE找出重复的数据,请联系管理员"),
    SKU_CODE_EMPTY(3007, "SKU CODE为空"),


    RECORD_LOCK(888, "数据正在执行更新,已被锁定,请稍后再试"),
    SYSTEM_RUNTIME_ERROR(999, "系统异常!");

    private Integer code;

    private String message;

    ResCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 通过code返回枚举
     *
     * @param code
     * @return
     */
    public static ResCode parse(Integer code) {
        ResCode[] values = values();
        for (ResCode value : values) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        throw new RuntimeException("Unknown code of ResultEnum");
    }
}

3、异常类


import com.centricsoftware.commons.em.ResCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 异常基类
 *
 * @author ZhengGong
 * @date 2019/6/18
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class BaseException extends RuntimeException {
    private Integer code;
    private String message;
    private Object data;

    public BaseException(ResCode resCode) {
        super(resCode.getMessage());
        this.code = resCode.getCode();
        this.message = resCode.getMessage();
    }

    public BaseException(ResCode resCode, Object data) {
        this(resCode);
        this.data = data;
    }

    public BaseException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BaseException(Integer code, String message, Object data) {
        this(code, message);
        this.data = data;
    }

    public BaseException(ResCode code, Throwable e) {
        this(code);
        data = e;
    }

    public ResCode getResCode() {
        return ResCode.parse(code);
    }


}

4、接口返回信息


import cn.hutool.core.util.StrUtil;
import com.centricsoftware.commons.em.ResCode;
import com.centricsoftware.commons.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.stringtemplate.v4.ST;

import java.net.URLEncoder;

/**
 * 接口返回信息
 *
 * @author ZhengGong
 * @date 2019/9/16
 */
@Slf4j
public class WebResponse {

    /**
     * 失败返回特定的消息实体
     *
     * @param code    错误代码
     * @param message 错误信息
     * @param data    具体消息实体
     * @return 消息实体ResEntity
     */
    public static ResEntity failure(Integer code, String message, Object data) {
        return ResEntity.builder().code(code).msg(message).data(data).success(false).build();
    }

    /**
     * 失败返回特定的消息实体
     *
     * @param code    错误代码
     * @param message 错误信息
     * @return 消息实体ResEntity
     */
    public static ResEntity failure(Integer code, String message) {
        return ResEntity.builder().code(code).msg(message).success(false).build();
    }

    /**
     * 失败返回特定的消息实体
     *
     * @param respCode 错误代码封装枚举类
     * @param data     具体消息实体
     * @return 消息实体ResEntity
     */
    public static ResEntity failure(ResCode respCode, Object data) {
        return getStringObjectMap(respCode, data, false);
    }

    /**
     * 失败返回特定的消息实体
     *
     * @param respCode 错误代码封装枚举类
     * @return 消息实体ResEntity
     */
    public static ResEntity failure(ResCode respCode) {
        return getStringObjectMap(respCode, false);
    }

    /**
     * 失败返回特定的消息实体
     *
     * @param e 错误基类
     * @return 消息实体ResEntity
     */
    public static <T extends BaseException> ResEntity failure(T e) {
        return failure(e.getCode(), e.getMessage(), e.getData());
    }

    /**
     * 成功返回特定的状态码和信息
     *
     * @param respCode 成功代码封装枚举类
     * @param data     具体消息实体
     * @return 消息实体ResEntity
     */
    public static ResEntity success(ResCode respCode, Object data) {
        return getStringObjectMap(respCode, data, true);
    }

    private static ResEntity getStringObjectMap(ResCode respCode, Object data, Boolean success) {
        return ResEntity.builder().code(respCode.getCode()).msg(respCode.getMessage()).data(data).success(success).build();
    }

    /**
     * 成功返回特定的状态码和信息
     *
     * @param respCode 成功代码封装枚举类
     * @return 消息实体ResEntity
     */
    public static ResEntity success(ResCode respCode) {
        return getStringObjectMap(respCode, true);
    }

    private static ResEntity getStringObjectMap(ResCode respCode, Boolean success) {
        return ResEntity.builder().code(respCode.getCode()).msg(respCode.getMessage()).success(success).build();
    }

    public static ResEntity failure(String message) {
        return ResEntity.builder().code(ResCode.ERROR.getCode()).msg(message).success(false).build();
    }

    public static ResEntity success(String message) {
        return ResEntity.builder().code(ResCode.SUCCESS.getCode()).msg(message).success(true).build();
    }

    public static ResEntity autoResponse(Object obj, String error) {
        if (StrUtil.isBlank(error)) {
            return ResEntity.builder().code(ResCode.SUCCESS.getCode()).msg(ResCode.SUCCESS.getMessage()).success(true).data(obj).build();
        } else {
            return ResEntity.builder().code(ResCode.ERROR.getCode()).msg(error).success(false).data(obj).build();
        }

    }

}

5、实例

public ResEntity pushModifyAttribute(){
  return WebResponse.failure(ResCode.SUCCESS, 值);
}

posted @   Retired-lad  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
点击右上角即可分享
微信分享提示