Result后端统一返回格式 !!!

Result后端统一返回格式

package com.hospital.manage.util;

import java.io.Serializable;

import com.hospital.manage.util.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 接口返回数据格式
 *
 */
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功!";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T result;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {
    }


    public Result<T> success(String message) {
        this.message = message;
        this.code = Constants.SC_OK_200;
        this.success = true;
        return this;
    }

    /**
     * 只返回状态
     * @return
     */
    public static<T> Result<T> ok() {
        Result<T> r = new Result();
        r.setSuccess(true);
        r.setCode(Constants.SC_OK_200);
        r.setMessage("成功");
        return r;
    }

    /**
     * 只返回状态, 不传data, 但传msg
     * @param msg
     * @return
     */

    public static<T> Result<T> ok(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(Constants.SC_OK_200);
        r.setMessage(msg);
        return r;
    }

    /**
     * 返回data, 不返回msg
     * @param data
     * @return
     */

    public static<T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(Constants.SC_OK_200);
        r.setResult(data);
        return r;
    }

    /**
     * 错误信息, 只返回msg
     * @param msg
     * @return
     */

    public static<T> Result<T> error(String msg) {
        return error(Constants.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    /**
     * 错误信息, 返回msg和code
     * @param code
     * @param msg
     * @return
     */

    public static<T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    /**
     * 服务器错误,数据库错误, 位置错误, 只传msg
     * @param message
     * @return
     */

    public Result<T> error500(String message) {
        this.message = message;
        this.code = Constants.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }


}

Constants

package com.hospital.manage.util;

/**
 * @author EDY
 * @since 2022/12/31 15:22
 */
public class Constants {
    /**
     * 成功
     */
    public static final Integer SC_OK_200 = 200;
    /**
     * 服务器异常
     */
    public static final int SC_INTERNAL_SERVER_ERROR_500 = 500;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子陌.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值