结果类
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* 结果类
*
* @author 韩朝阳
* @date 2020/05/15
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Result {
private Integer code;
private String message;
private Object data;
public Result(ResultEnum resultEnum, Object data) {
this.code = resultEnum.getCode();
this.message = resultEnum.getMessage();
this.data = data;
}
}
结果枚举
/**
* 结果枚举
*
* @author 韩朝阳
* @date 2020/05/15
*/
public enum ResultEnum {
SUCCESS(200, "成功"),
NO_PERMISSION(403, "权限不足"),
NOT_FOUND(404, "资源不存在"),
SERVER_ERROR(500, "服务器错误");
// ...
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}
结果工具类
/**
* 结果工具类
*
* @author 韩朝阳
* @date 2020/05/15
*/
public class ResultUtil {
public static Result success() {
return new Result(ResultEnum.SUCCESS, true);
}
public static Result success(Object data) {
return new Result(ResultEnum.SUCCESS, data);
}
public static Result error(ResultEnum resultEnum) {
return new Result(resultEnum, false);
}
}
网友评论