栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java封装返回方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java封装返回方法

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel(value = "请求结果响应体")
public class ResponseData implements Serializable{
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "响应状态回执码")
    private Integer code;

    @ApiModelProperty(value = "响应回执消息")
    private String msg;

    @ApiModelProperty(value = "数据体")
    private T data;

    @ApiModelProperty(value = "响应时间戳")
    private final long timestamps = System.currentTimeMillis();

    public ResponseData(Boolean success, Integer code, String message) {
        super();
        this.success = success;
        this.code = code;
        this.msg = message;
    }

    public static  ResponseData ok(ResponseCode responseCode) {
        return new ResponseData(true,responseCode.code,responseCode.msg);
    }

    public static  ResponseData ok(ResponseCode responseCode,Object data) {
        return new ResponseData(true,responseCode.code,responseCode.msg,data);
    }

    public static  ResponseData ok(ResponseCode responseCode,String message,Object data) {
        return new ResponseData(true,responseCode.code,message,data);
    }

    public static  ResponseData fail(ResponseCode responseCode) {
        return new ResponseData(false,responseCode.code,responseCode.msg);
    }

    public static  ResponseData fail(ResponseCode responseCode,String message) {
        return new ResponseData(false,responseCode.code,message);
    }
}
import java.util.HashMap;
import static java.util.Objects.isNull;



public class AjaxResult extends HashMap {
    private static final long serialVersionUID = 1L;

    private static final String CODE_TAG = "code";

    private static final String MSG_TAG = "msg";

    private static final String DATA_TAG = "data";

    public AjaxResult() {

    }

    public AjaxResult(int code,String msg){
        super.put("code",code);
        super.put("msg",msg);
    }

    public AjaxResult(int code, String msg, Object data){
        super.put("code",code);
        super.put("msg",msg);
        if(isNotNull(data)){
            super.put("data", data);
        }
    }

    // 操作成功
    public static AjaxResult success(){
        return success("操作成功");
    }

    public static AjaxResult success(Object data){
        return success("操作成功",data);
    }

    public static AjaxResult success(String msg){
        return success(msg,null);
    }

    public static AjaxResult success(String msg, Object data){
        return new AjaxResult(200,msg,data);
    }

    // 操作失败
    public static AjaxResult error(){
        return error("操作失败");
    }
    
    public static AjaxResult error(String msg){
        return error(msg,null);
    }
    
    public static AjaxResult error(Object data){
        return error("操作失败",data);
    }
    
    public static AjaxResult error(String msg, Object data){
        return new AjaxResult(500,msg,data);
    }

    public boolean isNotNull(Object data){
        return !isNull(data);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号