SpringBoot教程
SpringBoot入门案例
SpringBoot框架Web开发
SpringBoot非web应用程序
SpringBoot使用拦截器
SpringBoot中使用Servlet
SpringBoot中使用Filter
SpringBoot项目配置字符编码
SpringBoot打包与部署
SpringBoot使用Actuator
SpringBoot集成Thymeleaf模板
SpringBoot总结及综合案例
SpringBoot工程下使用Mybatis反向工程

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 主要是几个注解实现:

@PathVariable:获取url中的数据,该注解是实现RESTFul最主要的一个注解。

@PostMapping:接收和处理Post方式的请求

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

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

@GetMapping:接收get方式的请求

RESTful的优点

• 轻量,直接基于http,不再需要任何别的诸如消息协议,get/post/put/delete为CRUD操作

• 面向资源,一目了然,具有自解释性。

• 数据描述简单,一般以xml,json做数据交换。

• 无状态,在调用一个接口(访问、操作资源)的时候,可以不用考虑上下文,不用考虑当前状态,极大的降低了复杂度。

• 简单、低耦合

案例:使用RESTful风格模拟实现对学生的增删改查操作

项目名称:014-springboot-restful

该项目集成了MyBatis、spring、SpringMVC,通过模拟实现对学生的增删改查操作。

1.创建RESTfulController,并编写代码

@RestController
public class RESTfulController {

    /**
     * 添加学生
     * 请求地址:http://localhost:9090/014-springboot-restful/springBoot/student/wangpeng/23
     * 请求方式:POST
     * @param name
     * @param age
     * @return
     */
    @PostMapping(value = "/springBoot/student/{name}/{age}")
    public Object addStudent(@PathVariable("name") String name,
                             @PathVariable("age") Integer age) {

        Map retMap = new HashMap();
        retMap.put("name",name);
        retMap.put("age",age);


        return retMap;
    }

    /**
     * 删除学生
     * 请求地址:http://localhost:9090/014-springboot-restful/springBoot/student/1
     * 请求方式:Delete
     * @param id
     * @return
     */
    @DeleteMapping(value = "/springBoot/student/{id}")
    public Object removeStudent(@PathVariable("id") Integer id) {

        return "删除的学生id为:" + id;
    }

    /**
     * 修改学生信息
     * 请求地址:http://localhost:9090/014-springboot-restful/springBoot/student/2
     * 请求方式:Put
     * @param id
     * @return
     */
    @PutMapping(value = "/springBoot/student/{id}")
    public Object modifyStudent(@PathVariable("id") Integer id) {

        return "修改学生的id为" + id;
    }

    @GetMapping(value = "/springBoot/student/{id}")
    public Object queryStudent(@PathVariable("id") Integer id) {

        return "查询学生的id为" + id;
    }
}

2.使用Postman模拟发送请求,进行测试

3.总结:其实这里我们能感受到的好处

• 传递参数变简单了

• 服务提供者对外只提供了一个接口服务,而不是传统的CRUD四个接口

请求冲突的问题

项目名称:015-springboot-restful-url-conflict

• 改路径

• 改请求方式

创建RESTfulController类,结合Postman进行测试说明


@RestController
public class RESTfulController {


    /**
     * id:订单标识
     * status:订单状态
     * 请求路径:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/order/1/1001
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/springBoot/order/{id}/{status}")
    public Object queryOrder(@PathVariable("id") Integer id,
                             @PathVariable("status") Integer status) {

        Map map = new HashMap();

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

        return map;
    }

    /**
     * id:订单标识
     * status:订单状态
     * 请求路径:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1/order/1001
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/springBoot/{id}/order/{status}")
    public Object queryOrder1(@PathVariable("id") Integer id,
                              @PathVariable("status") Integer status) {
        Map map = new HashMap();

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

        return map;
    }

    /**
     * id:订单标识
     * status:订单状态
     * 请求路径:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1001/order/1
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/springBoot/{status}/order/{id}")
    public Object queryOrder2(@PathVariable("id") Integer id,
                              @PathVariable("status") Integer status) {
        Map map = new HashMap();

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

        return map;
    }

    /**
     * id:订单标识
     * status:订单状态
     * 请求路径:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1001/order/1
     * @param id
     * @param status
     * @return
     */
    @PostMapping(value = "/springBoot/{status}/order/{id}")
    public Object queryOrder3(@PathVariable("id") Integer id,
                              @PathVariable("status") Integer status) {
        Map map = new HashMap();

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

        return map;
    }




    /**
     * query1和query2两个请求路径会发生请求路径冲突问题
     * query3与query1和query2发生请求冲突
     * 注意:虽然两个路径写法改变了,但是由于传递的两个参数都是int值,所以不知道该交给哪个请求进行处理
     *      就会出现匹配模糊不清的异常,所以要想解决冲突,有两种方式:
     *      1.修改请求路径
     *      2.修改请求方式
     */
}

RESTful原则

• 增post请求、删delete请求、改put请求、查get请求

• 请求路径不要出现动词

例如:查询订单接口

/boot/order/1021/1(推荐)

/boot/queryOrder/1021/1(不推荐)

• 分页、排序等操作,不需要使用斜杠传参数

例如:订单列表接口 /boot/orders?page=1&sort=desc

一般传的参数不是数据库表的字段,可以不采用斜杠

全部教程