• SpringBoot的JSON工具类(java),用于前后端分离


    简介

    JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

    JSON工具类(自用)

    在这里插入图片描述

    ComonPage.java:用来封装PageHelper分页插件的数据

    package com.attendance.common;
    
    import com.github.pagehelper.PageInfo;
    import lombok.Data;
    
    import java.util.List;
    
    @Data
    public class CommonPage<T> {
        //当前页
        private int pageNum;
        //每页的数量
        private int pageSize;
        //当前页的数量
        private int size;
    
        //总记录数
        private long total;
    
        //总页数
        private int pages;
    
        //是否为第一页
        private boolean isFirstPage = false;
    
        //是否为最后一页
        private boolean isLastPage = false;
    
        //是否有前一页
        private boolean hasPreviousPage = false;
    
        //是否有下一页
        private boolean hasNextPage = false;
    
        //结果集
        private List<T> list;
    
        public static <T> CommonPage<T> restPage(List<T> list) {
            CommonPage<T> result = new CommonPage();
            PageInfo<T> pageInfo = new PageInfo(list);
            result.setPages(pageInfo.getPages());
            result.setPageNum(pageInfo.getPageNum());
            result.setPageSize(pageInfo.getPageSize());
            result.setSize(pageInfo.getSize());
            result.setTotal(pageInfo.getTotal());
            result.setFirstPage(pageInfo.isIsFirstPage());
            result.setHasNextPage(pageInfo.isHasNextPage());
            result.setHasPreviousPage(pageInfo.isHasPreviousPage());
            result.setLastPage(pageInfo.isIsLastPage());
            result.setList(pageInfo.getList());
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    就是用来封装下面这样的数据

        //    根据条件查学生信息
        @Override
        public CommonResult selectStudent(Student student, Pages pages) {
            PageHelper.startPage(pages.getPage(),pages.getNum());
            return CommonResult.success(CommonPage.restPage(studentMapper.selectStudent(student)));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Pages.java:用来装分页插件需要的数据

    package com.attendance.pojo;
    
    import lombok.Data;
    
    @Data
    public class Pages {
        private int page = 1;//第几页
        private int num = 5;//每页几条数据
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    CommonResult.java:封装JSON的工具类

    package com.attendance.common;
    
    import lombok.Data;
    
    @Data
    public class CommonResult<T> {
        private long code;
        private String message;
        private boolean flag;
        private T data;
    
        protected CommonResult() {
        }
    
        protected CommonResult(long code, boolean flag, String message, T data) {
            this.code = code;
            this.flag = flag;
            this.message = message;
            this.data = data;
        }
    
        public CommonResult(long code, boolean flag, String message) {
            this.code = code;
            this.flag = flag;
            this.message = message;
        }
    
        public static <T> CommonResult<T> success(T data) {
            return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), ResultCode.SUCCESS.getMessage(), data);
        }
    
        public static <T> CommonResult<T> success(T data, String message) {
            return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), message, data);
        }
    
        public static <T> CommonResult<T> success(String message) {
            return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), message);
        }
    
        public static <T> CommonResult<T> failed(IErrorCode errorCode) {
            return new CommonResult(errorCode.getCode(), errorCode.getFlag(), errorCode.getMessage(), (Object)null);
        }
    
        public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
            return new CommonResult(errorCode.getCode(), errorCode.getFlag(), message, (Object)null);
        }
    
        public static <T> CommonResult<T> failed(String message) {
            return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getFlag(), message, (Object)null);
        }
    
        public static <T> CommonResult<T> failed() {
            return failed((IErrorCode)ResultCode.FAILED);
        }
    
        public static <T> CommonResult<T> validateFailed() {
            return failed((IErrorCode)ResultCode.VALIDATE_FAILED);
        }
    
        public static <T> CommonResult<T> validateFailed(String message) {
            return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getFlag(), message, (Object)null);
        }
    
        public static <T> CommonResult<T> unauthorized(T data) {
            return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getFlag(), ResultCode.UNAUTHORIZED.getMessage(), data);
        }
    
        public static <T> CommonResult<T> forbidden(T data) {
            return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getFlag(), ResultCode.FORBIDDEN.getMessage(), data);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    IErrorCode:存放数据的接口

    package com.attendance.common;
    
    public interface IErrorCode {
        long getCode();
    
        boolean getFlag();
    
        String getMessage();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ResultCode.java:存放数据的实现类

    package com.attendance.common;
    
    public enum ResultCode implements IErrorCode {
        SUCCESS(200L, true, "操作成功"),
        FAILED(500L, false, "操作失败"),
        VALIDATE_FAILED(404L, false, "参数检验失败"),
        UNAUTHORIZED(401L, false, "暂未登录或token已经过期"),
        FORBIDDEN(403L, false, "没有相关权限");
    
        private long code;
        private boolean flag;
        private String message;
    
        private ResultCode(long code, boolean flag, String message) {
            this.code = code;
            this.flag = flag;
            this.message = message;
        }
    
        public long getCode() {
            return this.code;
        }
    
        public boolean getFlag() {
            return this.flag;
        }
    
        public String getMessage() {
            return this.message;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    具体操作代码

    Service:

    就是让方法的返回值变成CommonResult

        //    新增学生信息
        @Override
        public CommonResult selectStudentById(int id) {
            Student student = studentMapper.selectStudentById(id);
            return CommonResult.success(student,"查询成功!");
        }
    
        //    更改学生状态
        @Override
        public CommonResult updateStudentInfo(Student student) {
            int i = studentMapper.updateStudentInfo(student);
            if (i==1){
                return CommonResult.success("修改信息成功!");
            }else {
                return CommonResult.failed("修改数据失败!");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    Controller:

    //    更新学生基本信息
        @PostMapping("/updateStudentInfo")
        public CommonResult updateStudentInfo(@RequestBody Student student){
            return studentService.updateStudentInfo(student);
        }
    
    //    更改学生状态
        @PostMapping("/modifyStudentStatus")
        public CommonResult modifyStudentStatus(@RequestBody Student student){
            return studentService.modifyStudentStatus(student);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    发送JSON后的返回值(测试),这里我是那我另一个项目做的测试,反正返回值就是这个样子,工具类都可以调整,这里我用了Mybatis-Plus的分页插件,所以都没有使用CommonPage

    {
        "code": 0,
        "message": "查询成功!",
        "flag": true,
        "data": [
            {
                "id": 1,
                "stuName": "苏文绩",
                "stuNative": "重庆市铜梁区",
                "stuSex": "男",
                "stuPhone1": "17782299402",
                "stuPhone2": "19112799705",
                "classType": "文科",
                "stuIdentity": "500224200012050053",
                "stuAddress": "重庆市铜梁区东城街道玉泉花苑5-12-3",
                "specialType": "单亲家庭",
                "stuScore": "502",
                "stuMajor1": "软件工程",
                "stuMajor2": "计算机科学与技术",
                "status": 0,
                "contactContent": "",
                "version": 1,
                "createTime": "2022-06-23T09:50:26.000+00:00",
                "updateTime": "2022-06-27T03:55:51.000+00:00",
                "deleted": false
            },
            {
                "id": 2,
                "stuName": "谢汝雄",
                "stuNative": "重庆市江津区",
                "stuSex": "男",
                "stuPhone1": "17353192303",
                "stuPhone2": "11651561651",
                "classType": "文科",
                "stuIdentity": "500244200007031125",
                "stuAddress": "重庆市江津区",
                "specialType": "孤儿",
                "stuScore": "600",
                "stuMajor1": "自动化",
                "stuMajor2": "艺术与科技",
                "status": 1,
                "contactContent": "adasd",
                "version": 1,
                "createTime": "2022-06-23T09:52:18.000+00:00",
                "updateTime": "2022-06-27T04:42:21.000+00:00",
                "deleted": false
            },
            {
                "id": 3,
                "stuName": "张幸",
                "stuNative": "重庆市万州区",
                "stuSex": "男",
                "stuPhone1": "173151515",
                "stuPhone2": "12.1.21.1.15.15",
                "classType": "理科",
                "stuIdentity": "56156165165",
                "stuAddress": "重庆市万州区",
                "specialType": "孤儿,优扶家庭子女,家庭突发重大变故",
                "stuScore": "504",
                "stuMajor1": "计算机科学与技术",
                "stuMajor2": "艺术与科技",
                "status": 0,
                "contactContent": "",
                "version": 1,
                "createTime": "2022-06-23T09:53:06.000+00:00",
                "updateTime": "2022-06-26T08:47:18.000+00:00",
                "deleted": false
            },
            {
                "id": 4,
                "stuName": "赵云",
                "stuNative": "124124",
                "stuSex": "男",
                "stuPhone1": "12124124",
                "stuPhone2": "12512512",
                "classType": "1",
                "stuIdentity": "124124",
                "stuAddress": "123123123",
                "specialType": "孤儿,烈士子女,优扶家庭子女,建档立卡贫困家庭学生",
                "stuScore": "124124",
                "stuMajor1": "通信工程",
                "stuMajor2": "自动化",
                "status": 0,
                "contactContent": "",
                "version": 1,
                "createTime": "2022-06-23T10:29:57.000+00:00",
                "updateTime": "2022-06-26T08:52:32.000+00:00",
                "deleted": false
            },
            {
                "id": 5,
                "stuName": "张三",
                "stuNative": "重庆",
                "stuSex": "男",
                "stuPhone1": "12124",
                "stuPhone2": "12412",
                "classType": "1",
                "stuIdentity": "500224",
                "stuAddress": "124125125",
                "specialType": "孤儿,烈士子女,优扶家庭子女,建档立卡贫困家庭学生",
                "stuScore": "654",
                "stuMajor1": "市场营销",
                "stuMajor2": "信息工程",
                "status": 0,
                "contactContent": "",
                "version": 1,
                "createTime": "2022-06-23T11:02:43.000+00:00",
                "updateTime": "2022-06-26T09:18:58.000+00:00",
                "deleted": false
            }
        ],
        "count": 21
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
  • 相关阅读:
    【Qt】- 信号和槽函数
    CentOS安装Docker以及常用命令
    MySQL的预编译入门
    【Linux】多进程基础--信号
    CocosCreator播放音频并同步进度
    arm开发板
    汇编内中断
    【Linux】Linux常用操作命令(二)
    QT线程池的使用
    WiFi6的基础知识
  • 原文地址:https://blog.csdn.net/qq_57581439/article/details/125494876