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

ca.bitcoco.jsk.http.HttpResponseBody Maven / Gradle / Ivy

package ca.bitcoco.jsk.http;


/**
 * Core http request coreResponse format
 * {success:  ,result:   , error:   , message:  }
 *
 * @author Jiangqi Li
 */
public class HttpResponseBody {


    private T result;
    private String message;
    private APIError error;


    public HttpResponseBody() {

    }

    /**
     * Create an responseBody used in ResponseEntity.
     *
     * @param result       T result.
     * @param message      String message.
     * @param exception    Exception exception.
     * @param debugMessage String debugMessage.
     * @param errorCode    int errorCode.
     */
    private HttpResponseBody(T result, String message, Exception exception, String debugMessage, int errorCode) {
        this.result = result;
        this.message = message;
        if (exception != null) {
            this.error = new APIError(exception, debugMessage, errorCode);
        }
    }

    /**
     * Create an responseBody used in ResponseEntity.
     *
     * @param result       T result.
     * @param message      String message.
     * @param exception    Exception exception.
     * @param debugMessage String debugMessage.
     * @param errorCode    int errorCode.
     */
    private HttpResponseBody(T result, String message, Exception exception, String debugMessage, int errorCode, Object errorDetail) {
        this.result = result;
        this.message = message;
        if (exception != null) {
            this.error = new APIError(exception, debugMessage, errorCode);
            this.error.setErrorDetail(errorDetail);
        }
    }

    public static  HttpResponseBody result(T result) {
        return new HttpResponseBody(result, null, null, null, 10000);
    }

    public static  HttpResponseBody result(T result, String message) {
        return new HttpResponseBody(result, message, null, null, 10000);
    }


    public static  HttpResponseBody error(Exception error) {
        return new HttpResponseBody(null, null, error, null, 10000);
    }

    public static  HttpResponseBody error(Exception error, String message) {
        return new HttpResponseBody(null, message, error, null, 10000);
    }


    public static  HttpResponseBody error(Exception error, String message, String debugMessage) {
        return new HttpResponseBody(null, message, error, debugMessage, 10000);
    }

    public static  HttpResponseBody error(Exception error, String message, int errorCode) {
        return new HttpResponseBody(null, message, error, null, errorCode);
    }

    public static  HttpResponseBody error(Exception error, String message, String debugMessage, int errorCode) {
        return new HttpResponseBody(null, message, error, debugMessage, errorCode);
    }

    public static  HttpResponseBody error(Exception error, String message, String debugMessage, int errorCode, Object errorDetail) {
        return new HttpResponseBody(null, message, error, debugMessage, errorCode, errorDetail);
    }

    public String getMessage() {
        return message;
    }

    public APIError getError() {
        return error;
    }

    public T getResult() {
        return result;
    }


    public HttpResponseBody setResult(T result) {
        this.result = result;
        return this;
    }


    public HttpResponseBody setMessage(String message) {
        this.message = message;
        return this;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy