【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3

文章目录

    • 1. 基于Restful制作表现层接口
    • 2. 使用postman进行接口测试
    • 3. 表现层消息一致性处理(重要)
      • 3.1 创建结果集实体类R
      • 3.2 修改表现层接口
      • 3.3 使用postman进行接口测试

1. 基于Restful制作表现层接口

咱们表现层的开发使用基于Restful的表现层接口开发功能测试通过Postman工具进行。
Restful在之前的blog有介绍过,不熟悉的小伙伴可以回头找相关知识进行补充,postman是一个接口的测试工具,这里也不做过多解释,直接使用。

BookController

  1. PUT请求传递json数据,后台实用@RequestBody接收数据
  2. GET请求传递路径变量,后台实用@PathVariable接收数据
@RestController
@RequestMapping("/books")
public class BookController2 {

    @Resource
    private IBookService bookService;

    @GetMapping
    public List<Book> getAll(){
        return bookService.list();
    }

    @GetMapping("/{id}")
    public Book testGetById(@PathVariable int id){
        Book book = bookService.getById(id);
        return book;
    }

    @PutMapping
    public boolean update(@RequestBody Book book){
        return bookService.updateById(book);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable int id){
        return bookService.removeById(id);
    }

    @PostMapping
    public boolean insert(@RequestBody Book book){
        return bookService.save(book);
    }

    @GetMapping("{currentPage}/{pageSize}")
    public IPage<Book> testPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return bookService.getPage(currentPage,pageSize);
    }
}

2. 使用postman进行接口测试

接下来我们通过postman进行接口测试

  1. 查询全部,查询都是通过Get请求
    【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第1张图片

  2. 新增测试:POST请求
    新增的数据是通过JSON数据格式进行添加
    【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第2张图片

  3. 更新测试:PUT请求
    【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第3张图片

  4. 删除测试: DELETE请求
    【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第4张图片

  5. 分页请求: GET请求
    【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第5张图片

3. 表现层消息一致性处理(重要)

目前我们通过Postman测试后业务层接口功能是通的,但是这样的结果给到前端开发者会出现一个小问题。不同的操作结果所展示的数据格式差异化严重
比如上述测试,增删改返回值是true,id查询结果返回实体类对象,查询全部返回集合,这样的结果让前端人员看了是很容易让人崩溃的,必须将所有操作的操作结果数据格式统一起来,需要设计表现层返回结果的模型类,用于后端与前端进行数据格式统一,也称为前后端数据协议

3.1 创建结果集实体类R

创建实体类R,并编写构造方法。

@Data
public class R {
    private Boolean flag;
    private Object data;
    
	public R(){}

    public R(boolean flag){
        this.flag = flag;
    }

    public R(boolean flag,Object data){
        this.flag = flag;
        this.data = data;
    }
}

3.2 修改表现层接口

@RestController
@RequestMapping("/books")
public class BookController {

    @Resource
    private IBookService bookService;

    @GetMapping
    public R getAll() {
        /*R r = new R();
        List list = bookService.list();
        r.setFlag(true);
        r.setData(list);*/

        //凡是查询,flag统一设置成true
        return new R(true, bookService.list());
    }

    @GetMapping("/{id}")
    public R testGetById(@PathVariable int id) {
        return new R(true, bookService.getById(id));
    }

   /* @GetMapping("{currentPage}/{pageSize}")
    public R testPage(@PathVariable int currentPage, @PathVariable int pageSize) {
        IPage page = bookService.getPage(currentPage, pageSize);
        if(currentPage > page.getPages()){
            //如果当前页码大于总页码,重新查询一遍
            //加int强转是因为getPages返回long型,然后将getPage里面的参数currentPage换成page.getPages()
            page = bookService.getPage((int)page.getPages(), pageSize);
        }
        return new R(true,page);
        //return new R(true, bookService.getPage(currentPage, pageSize));
    }*/
    @GetMapping("{currentPage}/{pageSize}")
    public R testPage(@PathVariable int currentPage, @PathVariable int pageSize,Book book) {
        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        if(currentPage > page.getPages()){
            //如果当前页码大于总页码,重新查询一遍
            //加int强转是因为getPages返回long型,然后将getPage里面的参数currentPage换成page.getPages()
            page = bookService.getPage((int)page.getPages(), pageSize,book);
        }
        return new R(true,page);
    }

    @PutMapping
    public R update(@RequestBody Book book) {
        return new R(bookService.updateById(book));
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable int id) {
        return new R(bookService.deleteById(id));
    }

    @PostMapping
    public R insert(@RequestBody Book book) throws Exception {

        if (book.getName().equals("123")) throw new Exception();

        boolean flag = bookService.save(book);
        //return new R(bookService.save(book));
        return new R(flag, flag ? "添加成功^_^" : "添加失败");
    }
}

3.3 使用postman进行接口测试

我们简单测试几个,看看数据集的封装情况
根据id查询
【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第6张图片
新增请求
【基于springboot的SSMP整合案例1】表现层开发及使用postman测试3_第7张图片
通过上面接口的测试结果,可以看出,我们查询出来的数据集都已经封装,现在后端发送给前端的数据格式就统一了,免去了不少前端解析数据的麻烦。

总结:

  1. 设计统一的返回值结果类型便于前端开发读取数据

  2. 返回值结果类型可以根据需求自行设定,没有固定格式

  3. 返回值结果模型类用于后端与前端进行数据格式统一,也称为前后端数据协议

你可能感兴趣的:(SpringBoot项目集合,SpringBoot,spring,boot,mysql,java)