SpringBoot实现RESTful

一、认识 RESTful

REST (英文:Representational State Transfer ,简称 REST )
一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器
交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,REST
这个词,是 Roy Thomas Fielding 在他 2000 年的博士论文中提出的。
任何的技术都可以实现这种理念,如果一个架构符合 REST 原则,就称它为 RESTFul 架构

比如我们要访问一个 http 接口:http://localhost:8080/boot/order?id=1021&status=1
采用 RESTful 风格则 http 地址为:http://localhost:8080/boot/order/1021/1

二、Spring Boot 开发 RESTful

Spring boot 开发 RESTFul 主要是几个注解实现

1. @PathVariable

获取 url 中的数据
现该注解是实现 RESTful 最主要的一个注解

2. @PostMapping

接收和处理 Post 方式的请求

3. @DeleteMapping

接收 delete 方式的请求,可以使用 GetMapping 代替

4. @PutMapping

接收 put 方式的请求,可以用 PostMapping 代替

5. @GetMapping

接收 get 方式的请求

三、案例

创建11-springboot-restful项目,一个基本的springboot项目

1. 实现

1. 建立一个model,里面有Student

package com.md.springboot.model;

/**
 * @author MD
 * @create 2020-08-21 20:20
 */
public class Student {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在StudentController中

package com.md.springboot.web;

import com.md.springboot.model.Student;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author MD
 * @create 2020-08-21 20:21
 */

@RestController
public class StudentController {


    @RequestMapping(value = "/student")
    public Object student(Integer id , String name){
        Student student = new Student();
        student.setId(id);
        student.setName(name);

        return student;
    }



    @RequestMapping(value = "student/detail/{id}/{name}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("name") String name){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("name",name);

        return retMap;
    }
}

采用普通方式是这样的:http://localhost:8080/student?id=1001&name=pony

若采用这种风格之后:http://localhost:8080/student/detail/1001/pony

可以根据开发中的需要,是否使用这样的格式

2. 请求冲突的问题

如果在StudentController有这样的请求

    @RequestMapping(value = "student/detail/{id}/{name}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("name") String name){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("name",name);

        return retMap;
    }


//    和上面的请求路径冲突
    @RequestMapping(value = "student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") String status){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("status",status);

        return retMap;
    }

此时运行会报错,会出现路径冲突

此时有两种方式

  • 修改请求路径
  • 修改请求方式

第一个就直接把路径改了,肯定不会再冲突了,针对第二种方式,可以修改成这样

 @PostMapping(value = "student/detail/{id}/{name}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("name") String name){
       Student student = new Student();
       student.setId(id);
       student.setName(name);

       return student;
    }



    @GetMapping(value = "student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") String status){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("status",status);

        return retMap;
    }

一个使用post请求,一个使用get请求,这样就不会报错了

![](https://img2020.cnblogs.com/blog/1212924/202012/1212924-20201225222518548-1752399728.png

四、RESTful 原则

  1. 增 post 请求、删 delete 请求、改 put 请求、查 get 请求

  2. 请求路径不要出现动词

    例如:查询订单接口
    /boot/order/1021/1(推荐)
    /boot/queryOrder/1021/1(不推荐)

  3. 分页、排序等操作,不需要使用斜杠传参数
    例如:订单列表接口
    /boot/orders?page=1&sort=desc
    一般传的参数不是数据库表的字段,可以不采用斜杠

posted @ 2020-12-25 22:26  山丘i  阅读(3266)  评论(0编辑  收藏  举报