Title

后端返给前端统一结果集

第一种

  • 响应状态码
package com.zl.securitytest.common;

/**
 * 结果状态码集
 *
 * @author z
 * @date 2021-12-06 18:11
 */
public enum ResultCode {
    // 执行成功
    SUCCESS(200, "执行成功"),
    // 执行失败
    FAILED(500, "执行失败"),
    // 资源未找到
    NOT_FOUND(404, "资源未找到");

    public int CODE;
    public String MSG;

    private ResultCode(int code, String msg) {
        this.CODE = code;
        this.MSG = msg;
    }
    
    /**
     * 根据 code 获取 msg
     *
     * @param code
     * @return
     */
    public static String getMsg(int code) {
        ResultCode[] resultCode = values();
        for (ResultCode value : resultCode) {
            if (value.CODE == code) {
                return value.MSG;
            }
        }
        return "";
    }
}

  • 统一响应结果集封装类
package com.zl.securitytest.common;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * 统一应答
 *
 * @author z
 * @date 2021-12-06 10:21
 */
public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private String sysTime;
    private T data;

    public Result() {
    }

    public Result(Integer code, String msg, String sysTime, T data) {
        this.code = code;
        this.msg = msg;
        this.sysTime = sysTime;
        this.data = data;
    }

    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 String getSysTime() {
        return sysTime;
    }

    public void setSysTime(String sysTime) {
        this.sysTime = sysTime;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private static <T> Result<T> doInstance(int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(new HashMap<>(0));
        result.setSysTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return result;
    }

    public static <T> Result<T> ok() {
        return doInstance(ResultCode.SUCCESS.CODE, ResultCode.SUCCESS.MSG);
    }

    public static <T> Result<T> ok(T o) {
        Result<T> ok = ok();
        ok.setData(o);
        return ok;
    }

    public static <T> Result<T> ok(int code, String msg) {
        Result<T> ok = ok();
        ok.setCode(code);
        ok.setMsg(msg);
        return ok;
    }

    public static <T> Result<T> ok(T o, int code, String msg) {
        Result<T> ok = ok();
        ok.setCode(code);
        ok.setMsg(msg);
        ok.setData(o);
        return ok;
    }

    public static <T> Result<T> failed() {
        return doInstance(ResultCode.FAILED.CODE, ResultCode.FAILED.MSG);
    }

    public static <T> Result<T> failed(String msg) {
        Result<T> failed = failed();
        failed.setMsg(msg);
        return failed;
    }

    public static <T> Result<T> failed(int code, String msg) {
        Result<T> failed = failed();
        failed.setCode(code);
        failed.setMsg(msg);
        return failed;
    }

    public static <T> Result<T> failed(T o, int code, String msg) {
        Result<T> failed = failed();
        failed.setCode(code);
        failed.setMsg(msg);
        failed.setData(o);
        return failed;
    }

    public static IPage getPage(Integer pageNumber, Integer pageSize, List list) {
        IPage page = new Page<>(pageNumber, pageSize, list.size()).setRecords(ListUtil.page(pageNumber - 1, pageSize, list));
        return page;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", sysTime='" + sysTime + '\'' +
                ", data=" + data +
                '}';
    }
}

第二种

package com.zl.utils;

import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一应答
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	private R() {
		put("code", 200);
		put("msg", "success");
	}

	public static R ok() {
		return new R();
	}

	public static R ok(String msg) {
		R r = ok();
		r.put("msg", msg);
		return r;
	}
	
	public static R ok(Map<String, Object> map) {
		R r = ok();
		r.putAll(map);
		return r;
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R error() {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知错误,请联系管理员");
	}
	
	public static R error(String msg) {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
	}
	
	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}

注意:使用这种响应结果,在swagger文档上面显示响应结果为object,需要手动添加注解 @ApiResponses 即可,@ApiResponse 注解必须在 @ApiResponses 注解里,否则不会生效

    @ApiResponses({
        @ApiResponse(code = 200,message = "ok",response = User.class)
    })
posted @ 2021-12-06 18:40  快乐小洋人  阅读(76)  评论(0编辑  收藏  举报