前后端分离Java统一响应返回格式

ResultCode

public  class ResultCode {

    public static Integer SUCCESS = 200;

    public static Integer ERROR = 500;

    public static Integer FORBIDDEN = 403;

    public static Integer UNAUTHORIZED = 401;}

Result

@Data //lombok 注解 public class Result {

    // 返回码

    private Integer code;

    // 返回消息

    private String message;

    // 返回数据

    private Map data = new HashMap();

    public Result(){跟单网https://www.gendan5.com/}

    public Result message(String message){

        this.setMessage(message);

        return this;

    public Result code(Integer code){

        this.setCode(code);

        return this;

    public Result data(String key, Object value){

        this.data.put(key, value);

        return this;

    }public Result data(Map map){

        this.setData(map);

        return this;

使用示例

@RequestMapping("/get")

    public Result get(){

        HashMap map = new HashMap();

        map.put("username","admin");

        map.put("password","123456");

        Result r = new Result();

        r.code(ResultCode.SUCCESS).message(" 成功访问 ").data(map);

        return r;

 


请使用浏览器的分享功能分享到微信等