SpringBoot front-end and back-end separation unified return data format

1. Why do we need a unified return format

In the controller method, the data types returned to the front end are variable. For example, the verification code returns the Boolean data type, and the paging data returns the List data type. Multiple return types make the communication between the front-end and the back-end personnel complicated and unified. By stipulating a data format that is returned to the front end, it can greatly reduce tedious communication work and improve work efficiency


# 2. Two general Result classes **Description: Swagger3 and Lombok annotations are used here, depending on personal needs to modify ** **1.ResultEnumCode enumeration class**
package com.infoshare.return_result;


import lombok.Getter;

@Getter
public enum ResultCodeEnum {
    
    

    SUCCESS(true,20000,"成功"),
    UNKNOW_REASON(false,20001,"未知错误"),
    BAD_SQL_GRAMMER(false,21001,"sql语法错误"),
    JSON_PARSE_ERROR(false,21002,"json解析异常"),
    PARAM_ERROR(false,21003,"参数不正确"),
    FILE_UPLOAD_ERROR(false,21004,"文件上传错误"),
    EXCEL_DATA_IMPORT_ERROR(false,21005,"Excel数据导入错误"),
    ERROR_AUTH_CODE(false,21006,"验证码错误!"),
    ERROR_NOT_EXISTS_USER(false,21007,"用户不存在!"),
    ERROR_PASSWORD(false,21008,"密码错误,请重新输入!"),
    SUCCESS_LOGIN(true,20000,"登陆成功!"),
    SUCCESS_REGISTER(true,20000,"注册成功!");
    
    private Boolean status; //响应是否成功
    private Integer code; //返回码
    private String message; //返回消息

    ResultCodeEnum(Boolean status, Integer code, String message) {
    
    
        this.status = status;
        this.code = code;
        this.message = message;
    }

}


2.Result class

package com.infoshare.return_result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;

@Data
@ApiModel(value = "统一返回数据格式")
public class Result {
    
    

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data = new HashMap<String ,Object>();

    private Result(){
    
    }

    public static Result ok(){
    
    
        Result r = new Result();
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setSuccess(ResultCodeEnum.SUCCESS.getStatus());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    public static Result error(){
    
    
        Result r = new Result();
        r.setCode(ResultCodeEnum.UNKNOW_REASON.getCode());
        r.setSuccess(ResultCodeEnum.UNKNOW_REASON.getStatus());
        r.setMessage(ResultCodeEnum.UNKNOW_REASON.getMessage());
        return r;
    }

    public static Result ok(ResultCodeEnum codeEnum){
    
    
        Result r = new Result();
        r.setCode(codeEnum.getCode());
        r.setSuccess(codeEnum.getStatus());
        r.setMessage(codeEnum.getMessage());
        return r;
    }

    public static Result error(ResultCodeEnum codeEnum){
    
    
        Result r = new Result();
        r.setCode(codeEnum.getCode());
        r.setSuccess(codeEnum.getStatus());
        r.setMessage(codeEnum.getMessage());
        return r;
    }
    public Re
    sult data(Map<String,Object> map){
    
    
        this.setData(map);
        return this;
    }

    public Result data(String key,Object value){
    
    
        this.data.put(key,value);
        return this;
    }

    public Result message(String message){
    
    
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
    
    
        this.setCode(code);
        return this;
    }

    public Result success(Boolean success){
    
    
        this.setSuccess(success);
        return this;
    }

}


3. The controller layer returns Result example

Insert picture description here


4. Example of receiving Result data at the front end

Insert picture description here


5. The Swagger3 interface document shows the verification of the returned data format

Insert picture description here


to sum up

Using a unified data format to return, greatly improves the efficiency of independent development of the front-end and back-end, and also makes the overall development of the project concise. The back-end can customize some errors and success messages and status codes according to the requirements document and put them in ResultCodeEnum In the enumeration class, then call the Result.ok() or Result.error() static method to return the corresponding enumeration constant, making the maintenance of the project concise

Guess you like

Origin blog.csdn.net/weixin_43967679/article/details/108693789