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

cn.novelweb.tool.http.Result Maven / Gradle / Ivy

package cn.novelweb.tool.http;

import cn.novelweb.config.ConstantConfiguration;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.Optional;

/**
 * 

状态返回类

* 2019-10-30 02:00 * * @author Dai Yuanchuan **/ @ApiModel(value = "返回信息") @NoArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class Result implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "状态码") private String code; @ApiModelProperty(value = "描述") private String message; @ApiModelProperty(value = "对象") private T data; private Result(String code, String message) { this.code = code; this.message = message; } private Result(String code, String message, T obj) { this.code = code; this.message = message; this.data = obj; } /** * 判断返回是否为成功 * * @param result Result * @return 是否成功 */ public static boolean isSuccess(@Nullable Result result) { return Optional.ofNullable(result) .map(x -> ObjectUtils.nullSafeEquals(ConstantConfiguration.success, x.code)) .orElse(Boolean.FALSE); } /** * 判断返回是否为成功 * * @param result Result * @return 是否成功 */ public static boolean isNotSuccess(@Nullable Result result) { return !Result.isSuccess(result); } public static Result ok(T data) { return new Result<>(ConstantConfiguration.success, "请求成功", data); } public static Result ok() { return new Result<>(ConstantConfiguration.success, "请求成功"); } public static Result ok(String code, String message) { return new Result<>(code, message); } public static Result ok(int code, String message) { return new Result<>(String.valueOf(code), message); } public static Result ok(String code, String message, T data) { return new Result<>(code, message, data); } public static Result ok(int code, String message, T data) { return new Result<>(String.valueOf(code), message, data); } public static Result fail(T data) { return new Result<>(ConstantConfiguration.fail, "请求失败", data); } public static Result fail() { return new Result<>(ConstantConfiguration.fail, "请求失败", null); } public static Result fail(String message) { return new Result<>(ConstantConfiguration.fail, message, null); } public static Result fail(String code, String message) { return new Result<>(code, message); } public static Result fail(int code, String message) { return new Result<>(String.valueOf(code), message); } public static Result fail(String code, String message, T data) { return new Result<>(code, message, data); } public static Result fail(int code, String message, T data) { return new Result<>(String.valueOf(code), message, data); } public static Result authority(String message) { return new Result<>(ConstantConfiguration.noAuthority, message, null); } public static Result authority() { return new Result<>(ConstantConfiguration.noAuthority, "抱歉!您没有对应的权限", null); } public static Result refuse() { return new Result<>(ConstantConfiguration.refuse, "登录过期!请重新登录", null); } public static Result refuse(String message) { return new Result<>(ConstantConfiguration.refuse, message, null); } public static Result error(String message) { return new Result<>(ConstantConfiguration.systemError, message, null); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy