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

com.easy.core.domain.ApiResult Maven / Gradle / Ivy

The newest version!
package com.easy.core.domain;

import com.easy.core.enumx.FailureEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回结果封装
 */
@Data
public class ApiResult {

    private int code;

    private T data;

    private boolean success;

    private String message;

    public static ApiResult ok() {
        ApiVoidResult result = new ApiVoidResult();
        result.setSuccess(true);
        return result;
    }

    public static  ApiResult ok(T t) {
        ApiObjectResult result = new ApiObjectResult();
        result.setSuccess(true);
        result.setData(t);
        return (ApiResult) result;
    }

    public ApiResult data(T data) {
        this.data = data;
        return this;
    }

    public static ApiResult error() {
        ApiObjectResult result = new ApiObjectResult();
        result.setSuccess(false);
        return result;
    }

    public static ApiResult error(FailureEnum failureEnum) {
        ApiVoidResult result = new ApiVoidResult();
        result.setSuccess(false);
        result.setCode(failureEnum.getCode());
        result.setMessage(failureEnum.getMsg());
        return result;
    }

    public static ApiResult error(int code,String message) {
        ApiVoidResult result = new ApiVoidResult();
        result.setSuccess(false);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    @NoArgsConstructor
    public static class ApiObjectResult extends ApiResult {

    }

    @NoArgsConstructor
    public static class ApiVoidResult extends ApiResult {

    }
}