返回给前端数据的封装

返回格式如下:

{ "code": 200/400,

"msg": "成功"/"失败",

"total": n,

"data": [ {},{}]}

1.在common中新增Result 类,代码如下

java 复制代码
package com.xxx0523.common;
import lombok.Data;
@Data
public class Result {
    private int code;//编码 200.400
    private  String msg;//成功、失败
    private  Long total;//总记录数
    private  Object data;//数据
    private  String language;
    private static Result result (int code,String msg,String language,Long total,Object data){
        Result res =new Result();
        res.setLanguage(language);
        res.setData(data);
        res.setMsg(msg);
        res.setCode(code);
        res.setTotal(total);
        return  res;
    }
    public static  Result fail(){
        return result(400,"失败","中文",0L,null);
    }
    public static  Result suc(){
        return result(200,"成功","中文",0L,null);
    }
    public static  Result suc( Object data){
        return result(200,"成功","中文",0L,data);
    }
    public static  Result suc( Object data,Long total){
        return result(200,"成功","中文",total,data);
    }

}

2.更改用户控制类的代码

重启启动类并在在apipost中执行查看返回结果:

相关推荐
李子园的李2 分钟前
Java函数式接口——渐进式学习
java
汉得数字平台3 分钟前
汉得H-AI飞码——前端编码助手V1.1.2正式发布:融业务知识,提开发效能
前端·人工智能·智能编码
前端小万3 分钟前
Jenkins 打包崩了?罪魁是 package.json 里的 ^
前端·jenkins
编程小白gogogo4 分钟前
苍穹外卖前端环境搭建
前端
running up6 分钟前
Spring Bean生命周期- BeanDefinition 加载与 BeanFactoryPostProcessor BeanPostProcessor
java·后端·spring
光影少年7 分钟前
web端安全问题有哪些?
前端·安全
行走的陀螺仪14 分钟前
Vite & Webpack 插件/Loader 封装完全指南
前端·webpack·node.js·vite·前端工程化·打包构建
222you15 分钟前
Java线程的三种创建方式
java·开发语言
1024肥宅20 分钟前
浏览器网络请求 API:全面解析与高级封装(1)
前端·websocket·axios
脸大是真的好~21 分钟前
计算机408基础相关面试题-备用,不推荐
java