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

com.cloud.platform.common.domain.response.BaseResponse Maven / Gradle / Ivy

The newest version!
package com.cloud.platform.common.domain.response;

import com.cloud.platform.common.exception.BaseException;
import com.cloud.platform.common.exception.BaseExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description:
 * @Author: ZhouShuai
 * @Date: 2021-06-27 16:22
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResponse implements Serializable {

    private static final long serialVersionUID = -6276778785868535133L;

    private boolean success = false;
    private String errorCode;
    private String errorMessage;
    private String errorTips;
    private T model;

    public BaseResponse fail(BaseExceptionCode errorCode) {
        this.setSuccess(false);
        this.setErrorCode(errorCode.getErrorCode());
        this.setErrorMessage(errorCode.getErrorMessage());
        return this;
    }


    public BaseResponse fail(BaseException baseException) {
        this.setSuccess(false);
        this.setErrorCode(baseException.getErrorCode());
        this.setErrorMessage(baseException.getMessage());
        this.setErrorTips(baseException.getErrorTips());
        return this;
    }

    public BaseResponse success(T model) {
        this.setSuccess(true);
        this.setModel(model);
        return this;
    }

    public static  BaseResponse createSuccessResult(T model) {
        BaseResponse response = new BaseResponse();
        return response.success(model);
    }

    public static  BaseResponse createFailResult(BaseExceptionCode errorCode) {
        BaseResponse response = new BaseResponse();
        return response.fail(errorCode);
    }

    public static  BaseResponse createFailResult(BaseException baseException) {
        BaseResponse response = new BaseResponse();
        return response.fail(baseException);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy