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

com.github.hetianyi.common.RestResponse Maven / Gradle / Ivy

package com.github.hetianyi.common;

import java.io.Serializable;

/**
 * 作为Restful接口返回值载体,具有响应代码,响应信息,和数据载体三个基本字段。

* Example:

* * RestResponse.of(101, "server error", data);

* RestResponse.<UserPositionDO>builder().code(101).msg("server error").data(data).build()

*
* * @param any data type * @author Jason He * @version 1.0.6 * @since 1.0.0 * @date 2019-12-26 */ public class RestResponse implements Serializable { private static final long serialVersionUID = 3534961761119254698L; /** 响应代码 */ private int code; /** 响应信息 */ private String msg; /** 数据载体 */ private T data; /***************************************************************/ public RestResponse() { } public RestResponse(T data) { this.data = data; } public RestResponse(int code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } public RestResponse(Status status, T data) { this.code = status.getCode(); this.msg = status.getMsg(); this.data = data; } /*************************************************************/ public static RestResponse of(int code, String message, T data) { return new RestResponse(code, message, data); } public static RestResponse of(Status status, T data) { return new RestResponse(status, data); } /* public static RestResponse success() { return new RestResponse(0, "success", null); } public static RestResponse success(Status status) { return new RestResponse(status, null); } public static RestResponse success(T data) { return new RestResponse(0, "success", data); } public static RestResponse error() { return new RestResponse(101, "error", null); } public static RestResponse error(Status status) { return new RestResponse(status, null); } public static RestResponse error(T data) { return new RestResponse(101, "error", data); } */ public static RestResponseBuilder builder() { return new RestResponseBuilder<>(); } /** * Status接口方便自定义属于自己业务系统的一套响应码 */ public interface Status { int getCode(); String getMsg(); } /** * 链式方法构建RestResponse * @param 返回数据类型 */ public static class RestResponseBuilder { private int code; private String msg; private R data; private RestResponseBuilder() { } public RestResponseBuilder code(int code) { this.code = code; return this; } public RestResponseBuilder msg(String msg) { this.msg = msg; return this; } public RestResponseBuilder data(R data) { this.data = data; return this; } public RestResponse build() { return new RestResponse(code, msg, data); } } public int getCode() { return code; } public String getMsg() { return msg; } public T getData() { return data; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy