封装统一请求状态返回Result

1、测试数据

public class Test {

    public static void main(String[] args){

        System.out.println("Response.success(ErrorEnum.SUCCESS) = " + Response.success(ErrorEnum.SUCCESS));

        System.out.println("Response.success(ErrorEnum.FAIL) = " + Response.fail(ErrorEnum.FAIL));

        System.out.println("Response.fail() = " + Response.fail());

        System.out.println("Response.success() = " + Response.success());

    }

}

//Response.success(ErrorEnum.SUCCESS) = Response{code=200, msg='请求成功!', model=null}
//Response.success(ErrorEnum.FAIL) = Response{code=500, msg='请求失败!', model=null}
//Response.fail() = Response{code=500, msg='请求失败!', model=null}
//Response.success() = Response{code=200, msg='请求成功!', model=null}

 

2、创建Response类

public class Response<T> {

    private Integer code;

    private String msg;

    private T model;

    private volatile static Response response = null;

    private Response(){}

    public static Response getInstance(){
        if (response == null){
            synchronized (Response.class){
                if (response == null){
                    response =new Response();
                }
            }
        }
        return response;
    }


    public static Response fail(){
        Response response = Response.getInstance();
        response.setCode(ErrorEnum.FAIL.getCode());
        response.setMsg(ErrorEnum.FAIL.getMsg());
        return response;
    }

    public static Response fail(ErrorEnum errorEnum){
        Response response = Response.getInstance();
        response.setCode(errorEnum.getCode());
        response.setMsg(errorEnum.getMsg());
        return response;
    }

    public static <T> Response<T> fail(T data){
        Response<T> response = Response.getInstance();
        response.setCode(ErrorEnum.FAIL.getCode());
        response.setMsg(ErrorEnum.FAIL.getMsg());
        response.setModel(data);
        return response;
    }

    public static Response fail(int code, String msg) {
        Response response = Response.getInstance();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public static Response success(){
        Response response = Response.getInstance();
        response.setCode(ErrorEnum.SUCCESS.getCode());
        response.setMsg(ErrorEnum.SUCCESS.getMsg());
        return response;
    }

    public static Response success(ErrorEnum errorEnum){
        Response response = Response.getInstance();
        response.setCode(errorEnum.getCode());
        response.setMsg(errorEnum.getMsg());
        return response;
    }

    public static <T> Response<T> success(T data){
        Response<T> response = Response.getInstance();
        response.setCode(ErrorEnum.SUCCESS.getCode());
        response.setMsg(ErrorEnum.SUCCESS.getMsg());
        response.setModel(data);
        return response;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getModel() {
        return model;
    }

    public void setModel(T model) {
        this.model = model;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", model=" + model +
                '}';
    }
}

 

posted @ 2023-04-11 11:29  sowler  阅读(86)  评论(0编辑  收藏  举报