前后端数据协议
package com.tj.controller.utils;
import lombok.Data;
@Data
public class R {
private Boolean flag; //true表示处理成功,false表示请求处理失败
private Object data; //返回的数据
public R() {
}
public R(Boolean flag) {
this.flag = flag;
}
public R(Boolean flag, Object data) {
this.flag = flag;
this.data = data;
}
}
如下图
{
"flag": true,
"data": {
"records": [
{
"id": 10015,
"isuse": 0,
"user_id": "17751445454",
"password": "aa1acc1f79df777464acf9a42c52cefc07df905698532f59c150e163c013d871",
"user_name": "测试111"
},
{
"id": 10016,
"isuse": 0,
"user_id": "17751518888",
"password": "099277cf0e579118dc607c47c5cc57cfb058235a22b3cd13442c03677c6f3f2a",
"user_name": "李四"
},
{
"id": 10017,
"isuse": 0,
"user_id": "20220310",
"password": "4260b788c74885654f1fd8f742335ddd3dfe64beb721c6471988ee25f24b6e5b",
"user_name": "20220310"
},
{
"id": 10018,
"isuse": 0,
"user_id": "刘阳",
"password": "94edf28c6d6da38fd35d7ad53e485307f89fbeaf120485c8d17a43f323deee71",
"user_name": "刘阳"
},
{
"id": 10019,
"isuse": 0,
"user_id": "yetengchap",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"user_name": "allen"
}
],
"total": 84,
"size": 5,
"current": 2,
"orders": [],
"optimizeCountSql": true,
"searchCount": true,
"countId": null,
"maxLimit": null,
"pages": 17
}
}
异常数据
{
"timestamp": "2022-05-09T05:04:46.010+00:00",
"status": 405,
"error": "Method Not Allowed",
"path": "/users/"
}
后端处理异常,返回结构改变一下
package com.tj.controller.utils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//作为springMVC的异常处理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//拦截所有的异常信息
@ExceptionHandler
public R doException(Exception ex) {
//记录日志
//通知运维
//通知开发
ex.printStackTrace(); //控制台打印异常
return new R("服务器故障,请稍后再试!");
}
}