项目中自定义通用返回值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类
151 0
|
6月前
TS类型声明的方法之二
TS类型声明的方法之二
29 0
|
6月前
|
数据格式
统一封装类详解:Result
统一封装类详解:Result
1006 0
|
网络协议 Java 数据库连接
Springboot 通用返回类Result
Springboot 通用返回类Result
548 0
|
JavaScript 索引
ts扩展类型
接口 interface 含义:是指对协定进行定义的引用类型。其他类型实现接口,以保证它们支持某些操作。接口指定必须由类提供的成员或实现它的其他接口。与类相似,接口可以包含方法、属性、索引器和事件作为成员
ts扩展类型
|
前端开发
前端项目实战188-ts语法定义参数和参数返回值
前端项目实战188-ts语法定义参数和参数返回值
71 0
ts重点学习99-其他公共类型
ts重点学习99-其他公共类型
88 0
ts重点学习99-其他公共类型
ts重点学习31-类型断言
ts重点学习31-类型断言
81 0
ts重点学习31-类型断言
ts重点学习33-类型别名
ts重点学习33-类型别名
66 0
ts重点学习33-类型别名
ts重点学习41-函数接口
ts重点学习41-函数接口
70 0
ts重点学习41-函数接口