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

io.github.lyc8503.spring.starter.incantation.pojo.CommonResponse Maven / Gradle / Ivy

The newest version!
package io.github.lyc8503.spring.starter.incantation.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.github.lyc8503.spring.starter.incantation.exception.BizException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import io.github.lyc8503.spring.starter.incantation.exception.ErrorType;

@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class CommonResponse {
    /**
     * custom status code
     */
    private int code;
    /**
     * status message
     */
    private String msg;
    /**
     * data to be returned
     */
    private T data;


    /**
     * used to define http status code (not included in JSON response)
     */
    @JsonIgnore
    private int httpCode;

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

    public static  CommonResponse success(T data) {
        return success(data, 200);
    }

    public static  CommonResponse success(int httpCode) {
        return success(null, httpCode);
    }

    public static  CommonResponse success(T data, int httpCode) {
        return new CommonResponse<>(0, "success", data, httpCode);
    }

    public static  CommonResponse error(ErrorType type) {
        return new CommonResponse<>(type.getCode(), type.getMessage(), null, type.getHttpCode());
    }

    public static  CommonResponse error(ErrorType type, String msg) {
        return new CommonResponse<>(type.getCode(), msg, null, type.getHttpCode());
    }

    public static  CommonResponse error(BizException exception) {
        return new CommonResponse<>(exception.getCode(), exception.getMessage(), null, exception.getHttpCode());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy