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

dev.macula.boot.result.Result Maven / Gradle / Ivy

There is a newer version: 5.0.13
Show newest version
/*
 * Copyright (c) 2023 Macula
 *   macula.dev, China
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dev.macula.boot.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应消息报文
 *
 * @param   T对象
 * @author pangu
 */
@Data
public class Result implements Serializable {

    private boolean success;

    private String code;

    private String msg;

    private String cause;

    private T data;

    public static  Result success() {
        return success(null);
    }

    public static  Result success(T data) {
        return success(data, ApiResultCode.SUCCESS.getMsg());
    }

    public static  Result success(T data, String msg) {
        Result result = new Result<>();
        result.setSuccess(true);
        result.setCode(ApiResultCode.SUCCESS.getCode());
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static  Result failed() {
        return failed(ApiResultCode.FAILED);
    }

    public static  Result failed(ResultCode resultCode) {
        return failed(resultCode, null);
    }

    public static  Result failed(ResultCode resultCode, String cause) {
        // data是错误原因
        return failed(resultCode.getCode(), resultCode.getMsg(), cause);
    }

    public static  Result failed(String code, String msg, String cause) {
        // data是错误原因
        Result result = new Result<>();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(msg);
        result.setCause(cause);
        return result;
    }

    public static  Result failed(String code, String msg) {
        return failed(code, msg, null);
    }

    public static  Result judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy