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

club.zhcs.lina.web.response.Result Maven / Gradle / Ivy

The newest version!
package club.zhcs.lina.web.response;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 
 * @author Kerbores([email protected])
 *
 * @param 
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result {

    /**
     * 
     * @author Kerbores([email protected])
     *
     */
    public enum OperationState {
        /**
         * 成功
         */
        SUCCESS,
        /**
         * 失败
         */
        FAIL,
        /**
         * 异常
         */
        EXCEPTION;

    }

    @Default
    @Schema(description = "响应扩展数据", requiredMode = RequiredMode.NOT_REQUIRED)
    private NutMap ext = new NutMap();
    
    @Schema(description = "响应状态", requiredMode= RequiredMode.REQUIRED)
    @Default
    private OperationState state = OperationState.SUCCESS;
    
    @Schema(description = "错误信息列表",requiredMode= RequiredMode.AUTO)
    private String[] errors;
    
    @Schema(description = "响应数据", requiredMode= RequiredMode.AUTO)
    private T data;

    /**
     * 返回成功
     * 
     * @param 
     *            类型
     * @return 成功结果
     */
    public static  Result success() {
        return Result. builder()
                     .build();
    }

    /**
     * 携带数据返回成功
     * 
     * @param 
     *            数据泛型
     * @param t
     *            数据
     * @return 成功的结果
     */
    public static  Result success(T t) {
        return Result. builder()
                     .data(t)
                     .build();
    }

    /**
     * 返回失败及原因
     * 
     * @param 
     *            数据泛型
     * @param msgs
     *            失败原因
     * @return 失败的结果
     */
    public static  Result fail(String... msgs) {
        return Result. builder()
                     .state(OperationState.FAIL)
                     .errors(msgs)
                     .build();
    }

    /**
     * 返回异常及原因
     * 
     * @param 
     *            数据泛型
     * @param msgs
     *            异常原因
     * @return 异常的结果
     */
    public static  Result exception(String... msgs) {
        return Result. builder()
                     .state(OperationState.EXCEPTION)
                     .errors(msgs)
                     .build();
    }

    /**
     * 返回异常及原因
     * 
     * @param 
     *            数据泛型
     * @param msgs
     *            异常原因
     * @return 异常的结果
     */
    public static  Result exception(List msgs) {
        return Result. builder()
                     .state(OperationState.EXCEPTION)
                     .errors(msgs.toArray(new String[msgs.size()]))
                     .build();
    }

    /**
     * 返回异常及原因
     * 
     * @param 
     *            数据泛型
     * @param throwables
     *            异常
     * @return 异常的结果
     */
    public static  Result exception(Throwable... throwables) {
        return exception(Arrays.stream(throwables).map(Throwable::getMessage).collect(Collectors.toList()));
    }

    /**
     * 添加扩展数据
     * 
     * @param key
     *            数据key
     * @param value
     *            数据
     * @return 返回结果对象
     */
    public Result addExtData(String key, Object value) {
        getExt().setv(key, value);
        return this;
    }

    /**
     * 是否成功
     * 
     * @return 是否成功标识
     */
    public boolean isSuccess() {
        return getState() == OperationState.SUCCESS;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return Json.toJson(this, JsonFormat.forLook());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy