统⼀返回数据格式,分页,条件查询,分页条件查询,异常处理,⽇志处理
(三)
⽂章⽬录
⼀、统⼀返回数据格式
项⽬中我们会将响应封装成json返回,⼀般我们会将所有接⼝的数据格式统⼀, 使前端(iOS Android, Web)对数据的操作更⼀致、轻松。⼀般情况下,统⼀返回数据格式没有固定的格式,只要能描述清楚返回的数据状态以及要返回的具体数据就可以。但是⼀般会包含状态码、返回消息、数据这⼏部分内容
例如,我们的系统要求返回的基本数据格式如下:
列表:
分页:
没有返回数据:{  "success": true ,  "code": 20000,  "message": "成功",  "data": {    "items": [      {        "id": "1",        "name": "刘德华",        "intro": "毕业于师范⼤学数学系,热爱教育事业,执教数学思维6年有余"      }    ]  }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14{  "success": true ,  "code": 20000,  "message": "成功",  "data": {    "total": 17,    "rows": [      {        "id": "1",        "name": "刘德华",        "intro": "毕业于师范⼤学数学系,热爱教育事业,执教数学思维6年有余"      }    ]  }}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
⼆、创建统⼀结果返回类
1、在common模块下创建⼦模块common-utils
2、创建接⼝定义返回码
创建包com.atguigumonutils,创建接⼝ ResultCode.java
3、创建结果类
创建类 R.java {  "success": true ,  "code": 20000,  "message": "成功",  "data": {}}失败:{  "success": false ,  "code": 20001,  "message": "失败",  "data": {}}因此,我们定义统⼀结果{  "success": 布尔, //响应是否成功  "code": 数字, //响应码  "message": 字符串, //返回消息  "data": HashMap //返回数据,放在键值对中}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package com .atguigu monutils ;public interface ResultCode {    public static Integer  SUCCESS = 20000;    public static Integer  ERROR = 20001;}
1
2
3
4
5
三、统⼀返回结果使⽤
1、在service模块中添加依赖@Data public class  R {    @ApiModelProperty (value = "是否成功")    private  Boolean  success ;    @ApiModelProperty (value = "返回码")    private  Integer  code ;    @ApiModelProperty (value = "返回消息")    private  String  message ;    @ApiModelProperty (value = "返回数据")    private  Map <String , Object > data = new HashMap <String , Object >();    //构造⽅法私有化    private  R (){}    //定义两个⽅法    public static R ok (){        R r = new R ();        r .setSuccess (true );        r .setCode (ResultCode .SUCCESS );        r .setMessage ("成功");        return r ;    }    public static R error (){        R r = new R ();        r .setSuccess (false );        r .setCode (ResultCode .ERROR );        r .setMessage ("失败");        return r ;    }    //链式编程//    可以赋值    public R success (Boolean  success ){        this .setSuccess (success );        return this ;    }    public R message (String  message ){        this .setMessage (message );        return this ;    }    public R code (Integer  code ){        this .setCode (code );        return this ;    }    public R data (String  key , Object value ){        this .data .put (key , value );        return this ;    }    public R data (Map <String , Object > map ){        this .setData (map );        return this ;    }}
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  <dependency >            <groupId >com .xh </groupId >            <artifactId >common_utils </artifactId >            <version >0.0.1-SNAPSHOT </version >        </dependency >
1
2
3
4
5
2、修改Controller中的返回结果
列表
删除
测试成功
四、分页
1、MyBatisPlusConfig中配置分页插件 //查询    @GetMapping ("findAll")    @ApiOperation (value = "所有讲师列表")    public R list () {        //mp 中的list ⽅法  查询所有        List <EduTeacher > list = eduTeacherService .list (null );        return R .ok ().data ("items", list );    }
1
2
3
4
5
6
7
8//逻辑删除  记得逻辑删除插件    @ApiOperation (value = "根据ID 逻辑删除") //在swagger 上的描述    @DeleteMapping ("{id}")    public R removeById (@ApiParam (name = "id", value = "讲师ID", requir
ed = true ) @PathVariable String  id ) {  //ApiParam 在swagger 上的描述        boolean b = eduTeacherService .removeById (id );        if  (b ) {            return R .ok ();        } else  {            return R .error ();        }    }1
2
3
4
5
6
7
8
9
10
分页查询插件
11
12
2、分页Controller⽅法
EduTeacherController中添加分页⽅法
3、Swagger中测试
成功分页
五、条件查询
根据讲师名称name,讲师头衔level、讲师⼊驻时间gmt_create(时间段)查询
1、创建查询对象
创建com.ity.vo包,创建TeacherQuery.java条件查询对象/** * 分页插件 */@Bean public PaginationInterceptor paginationInterceptor () {    return new PaginationInterceptor ();}
1
2
3
4
5
6
7 //分页    @ApiOperation (value = "分页")    @GetMapping ("pageTeacher/{current}/{limit}")    public R pageListTeacher (@PathVariable long current ,  //当前页                            @PathVariable long limit ) {  //每页记录数        //创建page 对象        Page <EduTeacher > pageteacher = new Page <>(current , limit );        //调⽤⽅法实现分页        //调⽤⽅法 底层封装 会把所有数据封装到pageteacher 中        eduTeacherService .page (pageteacher , null );        long total = pageteacher .getTotal (); //总记录数        List <EduTeacher > records = pageteacher .getRecords (); //每页数据的集合        return R .ok ().data ("total", total ).data ("rows", records );    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14