JAVA-Actual-Combat-1

Java实战 (前后端返回类型) 1

简介

在前后端分离式的开发环境下,前后端的交互大多使用json格式的数据来交互。那么这个json格式的数据需要有一个统一的枚举工具类来实现,使后端能够轻易的将程序运行过程中的一些信息传递给前端开发人员。

代码示例

ResultCodeEnum

用于设定全局的返回枚举信息

package com.sevattal.common.base.result;
import lombok.Getter;
import lombok.ToString;
/**
 * @author Sevattal
 * @date 2021/4/7 0:15
 **/
@Getter
@ToString
public enum ResultCodeEnum {
    SUCCESS(true, 20000, "成功"),
    UNKNOWN_REASON(false, 20001, "未知错误"),
    BAD_SQL_GRAMMAR(false, 21001, "sql语法错误"),
    JSON_PARSE_ERROR(false, 21002, "json解析异常"),
    PARAM_ERROR(false, 21003, "参数不正确"),
    FILE_UPLOAD_ERROR(false, 21004, "文件上传错误"),
    FILE_DELETE_ERROR(false, 21005, "文件刪除错误"),
    EXCEL_DATA_IMPORT_ERROR(false, 21006, "Excel数据导入错误"),
    VIDEO_UPLOAD_TENCENT_ERROR(false,22008,"视频上传至腾讯云点播失败"),
    VIDEO_DELETE_TENCENT_ERROR(false,22009,"腾讯云点播视频删除失败"),
    VIDEO_SIGN_TENCENT_ERROR(false,22010,"腾讯云点播上传签名获取失败"),
    VIDEO_UPLOAD_ALIYUN_ERROR(false, 22001, "视频上传至阿里云失败"),
    VIDEO_UPLOAD_TOMCAT_ERROR(false, 22002, "视频上传至业务服务器失败"),
    VIDEO_DELETE_ALIYUN_ERROR(false, 22003, "阿里云视频文件删除失败"),
    FETCH_VIDEO_UPLOADAUTH_ERROR(false, 22004, "获取上传地址和凭证失败"),
    REFRESH_VIDEO_UPLOADAUTH_ERROR(false, 22005, "刷新上传地址和凭证失败"),
    FETCH_PLAYAUTH_ERROR(false, 22006, "获取播放凭证失败"),
    URL_ENCODE_ERROR(false, 23001, "URL编码失败"),
    ILLEGAL_CALLBACK_REQUEST_ERROR(false, 23002, "非法回调请求"),
    FETCH_ACCESSTOKEN_FAILD(false, 23003, "获取accessToken失败"),
    FETCH_USERINFO_ERROR(false, 23004, "获取用户信息失败"),
    LOGIN_ERROR(false, 23005, "用户名或密码不正确"),
    COMMENT_EMPTY(false, 24006, "评论内容必须填写"),
    PAY_RUN(false, 25000, "支付中"),
    PAY_UNIFIEDORDER_ERROR(false, 25001, "统一下单错误"),
    PAY_ORDERQUERY_ERROR(false, 25002, "查询支付结果错误"),
    ORDER_EXIST_ERROR(false, 25003, "课程已购买"),
    GATEWAY_ERROR(false, 26000, "服务不能访问"),
    MAIL_ERROR(false,28008,"邮箱发送失败"),
    CODE_ERROR(false, 28000, "验证码错误"),
    LOGIN_MOBILE_ERROR(false,28001,"手机号不正确"),
    LOGIN_DISABLED_ERROR(false, 28002, "该用户已被禁用"),
    REGISTER_USERINFO_ERROR(false, 28003, "手机号或者邮箱已被注册"),
    LOGIN_AUTH(false, 28004, "请先进行登录"),
    LOGIN_ACL(false, 28005, "没有权限"),
    SMS_SEND_ERROR(false, 28006, "短信发送失败"),
    SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL(false, 28007, "短信发送过于频繁"),
    ROLE_NAME_ERROR(false,28008,"角色名重复"),
    ADMIN_USER_DELEE_ERROR(false, 28009, "管理员角色不能删除");
    private final Boolean success;
    private final Integer code;
    private final String message;
    ResultCodeEnum(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }
}

R

真正用于 Controller 层的接口放回类型。

package com.sevattal.common.base.result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;
/**
 * @author Sevattal
 * @date 2021/4/7 0:16
 **/
@Data
@NoArgsConstructor
@ApiModel("全局统一返回结果")
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();
    public static R ok() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }
    public static R error() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
        return r;
    }
    public static R setResult(ResultCodeEnum resultCodeEnum) {
        R r = new R();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }
    public R success(Boolean success) {
        this.success(success);
        return this;
    }
    public R message(String message) {
        this.setMessage(message);
        return this;
    }
    public R code(Integer code) {
        this.setCode(code);
        return this;
    }
    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }
    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
Contents
  1. 1. Java实战 (前后端返回类型) 1
    1. 1.1. 简介
    2. 1.2. 代码示例
|