SpringBoot的返回信息的封装

需要安装lombok插件

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R {
    private int code;// 200 代表成功, 500 代表失败
    private String msg;// 返回提示信息
    private String status;// 要么成功,要么失败
    private Object data;// 返回数据
    public static final String SUCCESS = "success";// 成功
    public static final String FAIL = "fail";// 失败
    public static R ok() {
        return new R(200, "", SUCCESS, null);
    }
    public static R ok(String msg) {
        return new R(200, msg, SUCCESS, null);
    }
    public static R ok(Object data) {
        return new R(200, "", SUCCESS, data);
    }
    public static R ok(String msg, Object data) {
        return new R(200, msg, SUCCESS, data);
    }
    public static R error() {
        return new R(500, "", FAIL, null);
    }
    public static R error(String msg) {
        return new R(500, msg, FAIL, null);
    }
    public static R error(int code) {
        return new R(code, "", FAIL, null);
    }
    public static R error(int code, String msg) {
        return new R(code, msg, FAIL, null);
    }
}

PageResult的封装

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult<T> {
    //数据从前端传递
    private int pageNum;//当前页
    private int pageSize;// 每页显示多少条
    //数据从数据库中获取
    private List<T> list;// 每页显示数据
    private long total;;// 总条数
}

你可能感兴趣的:(spring,boot,后端,java)