项目中自定义通用返回值Result

简介: 项目中自定义通用返回值Result
通用的自定义相应类,轻便,兼容好,基本没有冗余字段

Result类


import java.io.Serializable;

/**
 * Description:
 *
 * @Author: Yanggc
 * DateTime: 10/22 13:52
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -3948389268046368059L;

    private Integer code;

    private String msg;

    private T data ;


    Result() {
    }

    private Result(T data) {
        this.code = 200;
        this.msg = "SUCCESS";
        this.data = data;
    }

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


    public static <T> Result<T> success() {
        Result result = new Result<T>();
        result.setResultCode(ResultCodeEnum.SUCCESS);
        return result;
    }

    public static <T> Result<T> failure(ResultCodeEnum resultCodeEnum) {
        Result result = new Result();
        result.setResultCode(resultCodeEnum);
        return result;
    }

    //自定义相应失败值
    public static <T> Result<T> failure(String failMsg) {
        Result result = new Result();
        result.setMsg(failMsg);
        result.setCode(201);
        return result;
    }


    public void setResultCode(ResultCodeEnum code) {
        this.code = code.code();
        this.msg = code.message();
    }

    // 自定义返回数据
    public Result<T> data(T data) {
        this.setData(data);
        return this;
    }

    // 自定义状态信息
    public Result msg(String message) {
        this.setMsg(message);
        return this;
    }

    // 自定义状态码
    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }





    /**
     *     getter setter
     */
    public Integer getCode() {
        return code;
    }

    private void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    private void setMsg(String msg) {
        this.msg = msg;
    }


    public T getData() {
        return data;
    }


    private void setData(T data) {
        this.data = data;
    }
}

枚举类:ResultCodeEnum

/**
 * Description:
 *
 * @Author: Yanggc
 * DateTime: 10/22 13:54
 */
public enum ResultCodeEnum {

    /* 状态码 */
    UNKNOWN_ERROR(404, "未知错误"),
    SUCCESS(200,"成功"),
    ERROR (201,"失败");

    private Integer code;

    private String message;

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

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

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

    public static String getMessage(String name) {
        for (ResultCodeEnum item : ResultCodeEnum.values()) {
            if (item.name().equals(name)) {
                return item.message;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCodeEnum item : ResultCodeEnum.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }


}

自定义分页响应类

/**
 * Description:
 *
 * @Author: Yanggc
 * DateTime: 10/23 12:24
 */
public class PageResult<T> extends Result<T> {

    //总记录数
    private Long total;

    //每页记录数
    private Long pageSize;

    //总页数
    private Long totalPages;

    //当前页码
    private Long currPage;

    private PageResult() {
    }

    public PageResult(Long total, Long pageSize, Long totalPages, Long currPage) {
        this.total = total;
        this.pageSize = pageSize;
        this.totalPages = totalPages;
        this.currPage = currPage;
    }

    public static <T> PageResult<T> success() {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setResultCode(ResultCodeEnum.SUCCESS);

        return pageResult;
    }

    public static <T> PageResult<T> failure(ResultCodeEnum resultCodeEnum) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setResultCode(resultCodeEnum);
        return pageResult;
    }





    /**
     *     getter setter
     */
    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Long totalPages) {
        this.totalPages = totalPages;
    }

    public Long getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Long currPage) {
        this.currPage = currPage;
    }

Mybatis-Plus 返回结果二次封装

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * Description:
 *  Mybatis-plus 分页条件结果二次封装
 *
 *
 * @Author: Yanggc
 * DateTime: 10/12 12:24
 */
public class PageResult<T> {

    private Integer code;

    private String msg;

    private Object data ;


    //总记录数
    private Long total;

    //每页记录数
    private Long pageSize;

    //总页数
    private Long totalPages;

    //当前页码
    private Long currPage;


    public void setResultCode(ResultCodeEnum code) {
        this.code = code.code();
        this.msg = code.message();
    }


    private PageResult() {
        //设置默认值
        this.total = 0L;
        this.pageSize = 0L;
        this.totalPages = 1L;
        this.currPage = 1L;
    }

    public static <T> PageResult<T> success(Page<T> page) {
        PageResult<Object> pageResult = new PageResult<>();
        pageResult.setResultCode(ResultCodeEnum.SUCCESS);
        pageResult.setData(page.getRecords());
        pageResult.setCurrPage(page.getCurrent());
        pageResult.setTotal(page.getTotal());
        pageResult.setPageSize(page.getSize());
        pageResult.setTotalPages(page.getPages());
        return (PageResult<T>) pageResult;
    }

    public static <T> PageResult<T> failure(String failMsg) {
        PageResult<T> pageResult = new PageResult<T>();
        // 201 自定义失败标识消息
        pageResult.setCode(201);
        pageResult.setMsg(failMsg);
        return pageResult;
    }

    public static <T> PageResult<T> failure(ResultCodeEnum resultCodeEnum) {
        PageResult<T> pageResult = new PageResult<T>();
        pageResult.setResultCode(resultCodeEnum);
        return pageResult;
    }


    // 自定义返回数据类型
    public PageResult<T> data(Object data) {
        this.setData(data);
        return this;
    }


    /**
     *     getter setter
     */
    public Integer getCode() {
        return code;
    }

    private void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public PageResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    private void setData(Object data) {
        this.data = data;
    }

    public Long getTotal() {
        return total;
    }

    private void setTotal(Long total) {
        this.total = total;
    }

    public Long getPageSize() {
        return pageSize;
    }

    private void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotalPages() {
        return totalPages;
    }

    private void setTotalPages(Long totalPages) {
        this.totalPages = totalPages;
    }

    public Long getCurrPage() {
        return currPage;
    }

    private void setCurrPage(Long currPage) {
        this.currPage = currPage;
    }
}

使用方法

对查询出来的Page对象进行一键封装
    public PageResult onsaleMembercardList(@RequestBody MemcardPageModel memberCardModel){
        Page<MemberCardProduct> memberCardProductPage = memcardService.searchMemcard(memberCardModel);
        return PageResult.success(memberCardProductPage);
    }

  • 返回结果
{
    "code": 200,
    "msg": "成功",
    "data": [{"id":1,"name": "张三"}],
    "total": 30,
    "pageSize": 1,
    "totalPages": 30,
    "currPage": 3
}
目录
相关文章
|
5月前
|
存储 JSON 前端开发
JavaWeb中用来定义统一的响应结果的result类
JavaWeb中用来定义统一的响应结果的result类
174 0
|
6月前
TS类型声明的方法之二
TS类型声明的方法之二
30 0
|
6月前
|
数据格式
统一封装类详解:Result
统一封装类详解:Result
1083 0
|
JavaScript
面试题-TS(十):如何处理可选属性和默认参数?
在TypeScript中,可选属性是指在定义接口或类时,指定某些属性不是必须的,可以存在也可以不存在。使用可选属性可以让我们定义更灵活的数据结构,允许对象中包含额外的属性,而不会报错。
|
网络协议 Java 数据库连接
Springboot 通用返回类Result
Springboot 通用返回类Result
562 0
|
前端开发
前端项目实战188-ts语法定义参数和参数返回值
前端项目实战188-ts语法定义参数和参数返回值
73 0
ts重点学习31-类型断言
ts重点学习31-类型断言
83 0
ts重点学习31-类型断言
ts重点学习49-函数得参数得处理方式
ts重点学习49-函数得参数得处理方式
85 0
ts重点学习49-函数得参数得处理方式
ts重点学习99-其他公共类型
ts重点学习99-其他公共类型
93 0
ts重点学习99-其他公共类型
ts重点学习41-函数接口
ts重点学习41-函数接口
70 0
ts重点学习41-函数接口