All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.mhuang.pacebox.springboot.protocol.Result Maven / Gradle / Ivy

package tech.mhuang.pacebox.springboot.protocol;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通用应答
 *
 * @author mhuang
 * @since 1.0.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {

    public static final int SUCCESS = 200;
    public static final int SYS_FAILD = 500;
    public static final int SYS_RESULT_FAILD = 202;
    public static final int TOKEN_EXPIRED = 401;

    public static final String SUCCESS_MSG = "操作成功";
    public static final String FAILD_MSG = "操作失败";
    public static final String TOKEN_EXPIRED_MSG = "当前用户登录已过期,请重新登录";
    public static final String TOKEN_IS_VALID_MSG = "非法访问,请重新登录";

    @ApiModelProperty(value = "返回状态")
    private int code;

    @ApiModelProperty(value = "提示出错的字段")
    @JsonInclude(Include.NON_NULL)
    private String field;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回的对象")
    @JsonInclude(Include.NON_NULL)
    private T data;

    @ApiModelProperty(value = "异常信息")
    @JsonInclude(Include.NON_NULL)
    private Object exceptionMsg;

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result success(T data) {
        this.code = SUCCESS;
        this.message = SUCCESS_MSG;
        this.data = data;
        return this;
    }

    public Result(T data) {
        this.data = data;
    }

    public static Result ok() {
        return new Result<>(SUCCESS, SUCCESS_MSG);
    }

    public static Result faild() {
        return new Result<>(SYS_FAILD, FAILD_MSG);
    }

    public static Result faild(String msg) {
        return new Result<>(SYS_FAILD, msg);
    }

    public static Result ok(Object data) {
        return new Result<>(SUCCESS, SUCCESS_MSG, data);
    }

    public static Result tokenExpired() {
        return new Result<>(TOKEN_EXPIRED, TOKEN_EXPIRED_MSG);
    }

    public static Result tokenValid() {
        return new Result<>(TOKEN_EXPIRED, TOKEN_IS_VALID_MSG);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy