Result.java
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import java.util.HashMap;
import java.util.Map;
@Data
@ToString
@Builder
@AllArgsConstructor
public class Result<T> {
/** 返回码 */
private String code;
/** 返回消息 */
private String message;
/** 数据时间 */
private Long time;
/** 返回数据 */
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
public Result(){
time=System.currentTimeMillis();
}
public Result message(String message){
this.setMessage(message);
return this;
}
public Result code(String code){
this.setCode(code);
return this;
}
public Result code(ResultCode resultCode){
this.setCode(resultCode.getCode());
this.setMessage(resultCode.getMessage());
return this;
}
public Result data(String key, Object value){
this.setData((T)new HashMap<>(1).put(key,value));
return this;
}
public Result data(Map<String, Object> map){
this.setData((T)map);
return this;
}
public Result data(Object value)
{
this.setData((T)value);
return this;
}
}
ResultCode.java
import lombok.Data;
import lombok.Getter;
/**
* 返回码类
*/
@Getter
public enum ResultCode {
SUCCESS("200", "ok"),
ERROR("500", "error"),
FORBIDDEN("403", "请求失败"),
UNAUTHORIZED("401", "认证失败"),
TOKEN_EXPIRED("901", "请求失败");
/**
* 错误类型码
*/
private String code;
/**
* 错误类型描述信息
*/
private String message;
ResultCode(String code, String message) {
this.code = code;
this.message = message;
}
}