前后端数据协议

  1. package com.tj.controller.utils;
  2. import lombok.Data;
  3. @Data
  4. public class R {
  5. private Boolean flag; //true表示处理成功,false表示请求处理失败
  6. private Object data; //返回的数据
  7. public R() {
  8. }
  9. public R(Boolean flag) {
  10. this.flag = flag;
  11. }
  12. public R(Boolean flag, Object data) {
  13. this.flag = flag;
  14. this.data = data;
  15. }
  16. }

如下图

  1. {
  2. "flag": true,
  3. "data": {
  4. "records": [
  5. {
  6. "id": 10015,
  7. "isuse": 0,
  8. "user_id": "17751445454",
  9. "password": "aa1acc1f79df777464acf9a42c52cefc07df905698532f59c150e163c013d871",
  10. "user_name": "测试111"
  11. },
  12. {
  13. "id": 10016,
  14. "isuse": 0,
  15. "user_id": "17751518888",
  16. "password": "099277cf0e579118dc607c47c5cc57cfb058235a22b3cd13442c03677c6f3f2a",
  17. "user_name": "李四"
  18. },
  19. {
  20. "id": 10017,
  21. "isuse": 0,
  22. "user_id": "20220310",
  23. "password": "4260b788c74885654f1fd8f742335ddd3dfe64beb721c6471988ee25f24b6e5b",
  24. "user_name": "20220310"
  25. },
  26. {
  27. "id": 10018,
  28. "isuse": 0,
  29. "user_id": "刘阳",
  30. "password": "94edf28c6d6da38fd35d7ad53e485307f89fbeaf120485c8d17a43f323deee71",
  31. "user_name": "刘阳"
  32. },
  33. {
  34. "id": 10019,
  35. "isuse": 0,
  36. "user_id": "yetengchap",
  37. "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
  38. "user_name": "allen"
  39. }
  40. ],
  41. "total": 84,
  42. "size": 5,
  43. "current": 2,
  44. "orders": [],
  45. "optimizeCountSql": true,
  46. "searchCount": true,
  47. "countId": null,
  48. "maxLimit": null,
  49. "pages": 17
  50. }
  51. }

异常数据

  1. {
  2. "timestamp": "2022-05-09T05:04:46.010+00:00",
  3. "status": 405,
  4. "error": "Method Not Allowed",
  5. "path": "/users/"
  6. }

后端处理异常,返回结构改变一下

  1. package com.tj.controller.utils;
  2. import org.springframework.web.bind.annotation.ExceptionHandler;
  3. import org.springframework.web.bind.annotation.RestControllerAdvice;
  4. //作为springMVC的异常处理器
  5. @RestControllerAdvice
  6. public class ProjectExceptionAdvice {
  7. //拦截所有的异常信息
  8. @ExceptionHandler
  9. public R doException(Exception ex) {
  10. //记录日志
  11. //通知运维
  12. //通知开发
  13. ex.printStackTrace(); //控制台打印异常
  14. return new R("服务器故障,请稍后再试!");
  15. }
  16. }