参考:Spring Boot的通用返回类Result
    Spring Boot的通用返回类Result_StillTogether的博客-CSDN博客

    在上一篇文章中,我们会发现Controller中函数的返回值类型是一个Result
    image.png

    一般来说,后台返回给前端的数据格式都是固定的,比如如下

    1. {
    2. "code": 5000,
    3. "data": [
    4. {
    5. "id": 8,
    6. "name": "zyl",
    7. "number": "2020112617",
    8. "createdAt": "2021-10-19T12:32:08.497Z",
    9. "updatedAt": "2021-10-20T02:57:34.580329Z",
    10. "deletedAt": null
    11. },
    12. {
    13. "id": 9,
    14. "name": "喻鑫瑞",
    15. "number": "123456789",
    16. "createdAt": "2021-10-19T14:02:32.875243Z",
    17. "updatedAt": "2021-10-19T14:02:32.875243Z",
    18. "deletedAt": null
    19. },
    20. {
    21. "id": 10,
    22. "name": "喻鑫瑞2",
    23. "number": "123456789",
    24. "createdAt": "2021-10-19T14:06:18.663413Z",
    25. "updatedAt": "2021-10-19T14:06:18.663413Z",
    26. "deletedAt": null
    27. },
    28. {
    29. "id": 11,
    30. "name": "fahfahi",
    31. "number": "215367164716",
    32. "createdAt": "2021-10-20T02:31:34.55309Z",
    33. "updatedAt": "2021-10-20T02:31:34.55309Z",
    34. "deletedAt": null
    35. },
    36. {
    37. "id": 12,
    38. "name": "fafa",
    39. "number": "215367164716",
    40. "createdAt": "2021-10-20T02:31:42.429517Z",
    41. "updatedAt": "2021-10-20T02:31:42.429517Z",
    42. "deletedAt": null
    43. }
    44. ],
    45. "msg": "success",
    46. "status": "ok"
    47. }
    1. {
    2. "code": 5000,
    3. "data": {
    4. "id": 8,
    5. "name": "zyl",
    6. "number": "2020112617",
    7. "createdAt": "2021-10-19T12:32:08.497Z",
    8. "updatedAt": "2021-10-20T02:57:34.580329Z",
    9. "deletedAt": null
    10. },
    11. "msg": "success",
    12. "status": "ok"
    13. }

    那么,如何在Java中实现这样的功能呢?
    只需要定义一个泛型Result类即可
    比如我们需要如下的返回结果

    1. {
    2. "code": 5000,
    3. "msg": "success",
    4. "status": "ok",
    5. "data": [
    6. {
    7. "name": "zyl3",
    8. "number": "123456"
    9. },
    10. {
    11. "name": "zyl4",
    12. "number": "123456"
    13. },
    14. {
    15. "name": "yxr",
    16. "number": "2020112617"
    17. }
    18. ]
    19. }

    那么对应的字段即是:

    字段 类型 作用
    code Integer 业务状态码
    msg String 返回的提示信息
    status String 返回的状态信息
    data T 数据

    对应的代码实现:

    1. package com.example.demo2.springbootmybatis.utils;
    2. public class Result<T> {
    3. private Integer code;
    4. private String msg;
    5. private String status;
    6. private T data;
    7. public Result(Integer code, String msg, String status, T data) {
    8. this.code = code;
    9. this.msg = msg;
    10. this.status = status;
    11. this.data = data;
    12. }
    13. public Result(Integer code, String msg, String status) {
    14. this.code = code;
    15. this.msg = msg;
    16. this.status = status;
    17. }
    18. public Integer getCode() {
    19. return code;
    20. }
    21. public void setCode(Integer code) {
    22. this.code = code;
    23. }
    24. public String getMsg() {
    25. return msg;
    26. }
    27. public void setMsg(String msg) {
    28. this.msg = msg;
    29. }
    30. public String getStatus() {
    31. return status;
    32. }
    33. public void setStatus(String status) {
    34. this.status = status;
    35. }
    36. public T getData() {
    37. return data;
    38. }
    39. public void setData(T data) {
    40. this.data = data;
    41. }
    42. @Override
    43. public String toString() {
    44. return "Result{" +
    45. "code=" + code +
    46. ", msg='" + msg + '\'' +
    47. ", status='" + status + '\'' +
    48. ", data=" + data +
    49. '}';
    50. }
    51. }

    那么我们在Controller中的函数返回值类型就可以使用我们的Result类
    比如这样:

    1. /**
    2. * 查询所有
    3. * */
    4. @GetMapping("/all")
    5. public Result<List<Test>> getAll(){
    6. List<Test> list = testService.getAll();
    7. return new Result<>(5000,"success","ok",list); //返回一个Result对象,Java会自动转换成JSON对象
    8. }

    如下图所示:
    image.png