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

com.blade.mvc.ui.RestResponse Maven / Gradle / Ivy

There is a newer version: 2.0.15.RELEASE
Show newest version
package com.blade.mvc.ui;

import com.blade.kit.DateKit;
import com.blade.kit.StringKit;
import lombok.Builder;
import lombok.Data;

/**
 * RestResponse
 *
 * @param 
 * @since 2.0.2-beta
 */
@Data
public class RestResponse {

    /**
     * Server response data
     */
    private T payload;

    /**
     * The request was successful
     */
    private boolean success;

    /**
     * Error message
     */
    private String msg;

    /**
     * Status code
     */
    @Builder.Default
    private int code = 0;

    /**
     * Server response time
     */
    private long timestamp;

    public RestResponse() {
        this.timestamp = DateKit.nowUnix();
    }

    public RestResponse(boolean success) {
        this.timestamp = DateKit.nowUnix();
        this.success = success;
    }

    public RestResponse(boolean success, T payload) {
        this.timestamp = DateKit.nowUnix();
        this.success = success;
        this.payload = payload;
    }

    public T getPayload() {
        return payload;
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMsg() {
        return msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public RestResponse peek(Runnable runnable) {
        runnable.run();
        return this;
    }

    public RestResponse success(boolean success) {
        this.success = success;
        return this;
    }

    public RestResponse payload(T payload) {
        this.payload = payload;
        return this;
    }

    public RestResponse code(int code) {
        this.code = code;
        return this;
    }

    public RestResponse message(String msg) {
        this.msg = msg;
        return this;
    }

    public static  RestResponse ok() {
        return new RestResponse().success(true);
    }

    public static  RestResponse ok(T payload) {
        return new RestResponse().success(true).payload(payload);
    }

    public static  RestResponse ok(T payload, int code) {
        return new RestResponse().success(true).payload(payload).code(code);
    }

    public static  RestResponse fail() {
        return new RestResponse().success(false);
    }

    public static  RestResponse fail(String message) {
        return new RestResponse().success(false).message(message);
    }

    public static  RestResponse fail(int code, String message) {
        return new RestResponse().success(false).message(message).code(code);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy