SpringBoot 通用返回类Result

博客参考文章:https://www.cnblogs.com/yui66/p/9615709.html

在前后端分离的项目中,为了统一接口返回参数,需要封装一个通用的返回类Result。

1、编写 Result 类(泛型)

package com.lin.result;

public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public Result setCode(ResultEnum resultEnum) {
        this.code = resultEnum.code;
        return this;
    }
}

2、定义枚举类

package com.lin.result;

public enum  ResultEnum {
    /**
     * 成功
     */
    SUCCESS(200),
    /**
     * 失败
     */
    FAIL(400),
    /**
     * 接口不存在
     */
    NOT_FOUND(404),
    /**
     * 服务器内部错误
     */
    INTERNAL_SERVER_ERROR(500);
    public int code;
    ResultEnum(int code) {
        this.code = code;
    }
}

可以根据实际需要增加识别码。

3、编写工具类封装方法

package com.lin.result;

public class ResultUtil {

    public static <T> Result<T>  defineSuccess(Integer code, T data) {
        Result result = new Result<>();
        return result.setCode(code).setData(data);
    }

    public static <T> Result<T> success(T data) {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS).setData(data);
        return result;
    }

    public static <T> Result<T> fail(String msg) {
        Result result = new Result();
        result.setCode(ResultEnum.FAIL).setMsg(msg);
        return result;
    }

    public static <T> Result<T> defineFail(int code, String msg){
        Result result = new Result();
        result.setCode(code).setMsg(msg);
        return result;
    }

    public static <T> Result<T> define(int code, String msg, T data){
        Result result = new Result();
        result.setCode(code).setMsg(msg).setData(data);
        return result;
    }
}

根据实际需求增删相关方法。

4、编写测试接口

    @GetMapping(value = "/findById")
    public Result findById(@RequestParam Long id) {
        User user = userService.findById(id);
        return ResultUtil.success(user);
    }

5、返回结果如下

{
    "code": 200,
    "msg": null,
    "data": {
        "id": 3,
        "name": "Tom",
        "age": 28,
        "email": "test3@baomidou.com",
        "createTime": "2020-08-17T01:51:35.000+00:00",
        "updateTime": "2020-08-17T02:07:02.000+00:00",
        "version": 1,
        "deleted": 0
    }
}
posted @ 2020-09-17 16:53  MyDistance  阅读(9873)  评论(0编辑  收藏  举报