基础开发框架搭建2---springbooot统一返回格式封装

104 阅读2分钟

基础开发框架搭建2—springbooot统一返回格式封装

1.为什么要对后端数据进行统一格式封装

平时开发过程中,后端返回数据给前端,以根据学生ID查询学生成绩集合为例,小白阶段开发可能会返回List<成绩>,但是假如传入的学生ID不存在呢?返回空的集合吗?若返回空的集合如何区分“不存在该学生”和“该学生无成绩信息”两种情况呢?

此时就需要将我们后端返回前端的数据的格式进行统一规范封装,一般包含请求是否成功、异常信息(可为空)、数据。

封装之后我们再来看上面的问题,后端即可返回:

  • 返回格式:{‘请求是否成功’,‘异常信息’,‘数据’}
  • {‘请求成功’, null, List<成绩>} :有学生信息
  • {‘请求成功’, ‘学生不存在’, null} :学生不存在
  • {‘请求成功’,null,空List} :学生成绩为空
2.统一返回格式类ResponseData
package com.spboot.merge.core.reponseDate;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @ClassName ResponseData
 * @description 统一返回格式
 * @author hellozhaoxudong@163.com
 * @date 2019/4/26 18:04
 * @version 1.0
 * @since JDK 1.8
 */
public class ResponseData {

    /** 调用成功标识 **/
    private boolean success;

    /** 调用异常编码 **/
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String errorCode;

    /** 调用异常信息 **/
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String errorMsg;

    /** 返回的数据 **/
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object datas;


    public ResponseData(){
        setSuccess(true);
    }

    public ResponseData(boolean success){
        setSuccess(success);
    }

    public ResponseData(String errorMsg){
        setSuccess(false);
        setErrorMsg(errorMsg);
    }

    public ResponseData(String errorCode, String errorMsg){
        setSuccess(false);
        setErrorCode(errorCode);
        setErrorMsg(errorMsg);
    }

    public ResponseData(Object datas){
        setSuccess(true);
        setDatas(datas);
    }

    /** getter & setter **/
    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Object getDatas() {
        return datas;
    }

    public void setDatas(Object datas) {
        this.datas = datas;
    }
}
3.Controller接口
package com.spboot.merge.example;

import com.spboot.merge.core.Exception.BaseException;
import com.spboot.merge.core.reponseDate.ResponseData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ExampleController
 * @description 示例
 * @author hellozhaoxudong@163.com
 * @date 2019/4/28 11:55
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/api/example")
public class ExampleController {


    /**
     * responseDataExample : 示例:统一数据返回格式
     *
     * @author hellozhaoxudong@163.com
     * @version 1.0
     * @date 2019/4/28 11:57
     * @param
     * @return org.springframework.http.ResponseEntity<java.util.List<com.spboot.merge.activi.dto.AreaInfo>>
     * @since JDK 1.8
     */
    @GetMapping(value = "/responseDate")
    public ResponseData responseDataExample(){
        List<ExampleDto> returnData = new ArrayList<ExampleDto>();

        ExampleDto dto = new ExampleDto();
        dto.setExampleId(1L);
        dto.setExampleName("name1");
        returnData.add(dto);

        dto = new ExampleDto();
        dto.setExampleId(2L);
        dto.setExampleName("name2");
        returnData.add(dto);

        return new ResponseData(returnData);
    }
}
4.效果

在这里插入图片描述

5.前端相关

我们规范好后端的返回之后,前端在接收时即可对请求之后的数据进行统一处理,例如每次请求之后,对返回的数据都先判断if(success == false),即表示有异常,就要将异常信息errorMsg展示出来给用户。

比较常见的方案就是将发送请求封装起来,返回的数据进行初步判断之后再return。当然若是报错前端有其他的处理逻辑,就不能使用封装之后的请求了。

6.拓展

怎样方便的将异常信息写入统一返回格式ResponseData呢?当我们抛出一个Exception的时候自动返回给前端含有异常信息的ResponseData是比较好的解决方案。下一篇就写一下如何《统一处理异常返回信息》