首页 > 其他 > 详细

统一资源返回结果集

时间:2021-04-30 21:10:02      阅读:21      评论:0      收藏:0      [点我收藏+]
package com.cherish.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

//统一返回结果的类
@Data
public class ResultData {
    @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>();

    //构造方法私有化,别人就不能new,直接调用暴露的方法

    private ResultData(){}

    //成功的静态方法

    public static ResultData ok(){
        ResultData result=new ResultData();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("成功");
        return result;
    }

    //失败的静态方法

    public static ResultData error(){
        ResultData result=new ResultData();
        result.setCode(ResultCode.ERROR);
        result.setMessage("失败");
        result.setSuccess(false);
        return result;
    }


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

    public ResultData code(Integer code){
        this.setCode(code);
        return this;
    }
    public ResultData message(String message){
        this.setMessage(message);
        return this;
    }

    public ResultData data(String key ,Object value){
        this.data.put(key, value);
        return this;
    }
    //直接传递一个map进去
    public ResultData data(Map<String ,Object> map){
        this.setData(map);
        return this;
    }

}

统一资源返回结果集

原文:https://www.cnblogs.com/cherish-code/p/14722892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!