接口架构风格-RESTful

接口架构风格-RESTful

这里的接口是指API(应用程序接口)

API(Application Programming Interface,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。 用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。

架构风格 : 简单来讲就是api的组织方式(样子)
例如: http://localhost:8081/mytrans/addStudent?name=lisi&age=26

RESTful架构风格

什么是REST

中文名叫做"表现层状态转移",它并不是一种标准,而是一种软件架构设计的风格,它只是提出了组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则时间的接口可以更简洁更有层次.
比如我们访问一个http接口 : http://localhost:8080/boot/order?id=1021&status=1
采用RESTful的风格地址为 : http://localhost:8080/boot/order/1021/1

表现层状态转移 :
表现层 : 就是视图层,显示资源的,通过视图页面等等显示操作资源的结果
状态 : 就是指资源的变换
转移 : 资源可以变化,比如新增学生,就是new的状态,删除等http动作改变资源,即转移

REST中的要素 :
用REST表示资源和对资源的操作,在互联网中,表示资源或者操作使用的是url的方式表示的.

对资源的操作 :

  • 查询资源 : 看,通过url找到资源
  • 更新资源 : 更新,编辑
  • 删除资源 : 去除
  • 新增资源 : 添加

资源使用URL表示,使用http中的动作(请求方式)表示对资源的操作(CRUD)

  • GET : 查询资源 – sql select
    查询单个学生 :
    http://localhost:8080/myboot/student/1001
    http://localhost:8080/myboot/student/1001/1
    查询多个学生 :
    http://localhost:8080/myboot/students/1001/1002
  • POST : 创建资源 – sql insert
    http://localhost:8080/myboot/student
    在post请求中传递参数
<form action="http://localhost:8080/myboot/student" method="post">
	姓名 : <input type="text" name="name" />
	年龄 : <input type="text" name="age" />
</form>
  • PUT : 更新资源 – sql updata
    更新学生1的资源
<form action="http://localhost:8080/myboot/student/1" method="post">
	姓名 : <input type="text" name="name" />
	年龄 : <input type="text" name="age" />
	<input type="hiden" name="_method" value="PUT" />
</form>
  • DELETE : 删除资源 – sql delete
<a herf="http://localhost:8080/myboot/student/1" >删除</a>

对于浏览器,默认支持GET和POST,不支持PUT和DELETE,但是我们可以通过别的方式间接支持,即使用POST方式模拟PUT和DELETE

需要分页的,排序的参数等这些无关紧要的信息,依然可以放在url的后面,例如:
http://localhost:8080/myboot/students?page=1&pageSize=20

一句话表示REST : 用url表示资源,用http动作操作资源

RESTful注解

注解名注解含义
@PathVariable从url中获取数据
@GetMapping支持get请求,等同于@RequestMapping(method=RequestMethod.GET)
@PostMapping支持Post请求,等同于@RequestMapping(method=RequestMethod.POST)
@PutMapping支持put请求,等同于@RequestMapping(method=RequestMethod.PUT)
@DeleteMapping支持delete请求,等同于@RequestMapping(method=RequestMethod.DELETE)
@RestController符合注解,是@Controller和@ResponseBody的组合,表示当前类中的所有方法都加入了@ResponseBody

下面通过一个controller来测试 :

import org.springframework.web.bind.annotation.*;

@RestController
public class MyRestController {

    // 学习注解的使用

    // 查询id=1001的学生

    /**
     * @PathVarable(路径变量) : 获取url中的数据
     *          属性 :
     *              value : 定义路径变量名的,放在控制器方法形参的前面,可以省略
     *                      PathVariable(value = "变量名")等价于PathVariable("变量名")
     */
    /*
        比如url为http://localhost:8081/myboot/student/1002
        这里的{stuId}就是1002,并将1002赋值给方法参数studentId
     */
    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable(value = "stuId") Integer studentId) {

        return "查询学生id=" + studentId;
    }

    /**
     * 创建资源的, Post请求
     */
    @PostMapping("/student/{name}/{age}")
    public String createStudent(@PathVariable("name") String name,
                                @PathVariable("age") Integer age) {

        return "创建了名字为" + name + "年龄为" + age + "的学生对象";
    }

    /**
     * 更新资源
     * 当路径变量的名称和形参名一样,PathVariable之后的内容都可以不写
     */
    @PutMapping("/student/{id}/{age}")
    public String modifyStudent(@PathVariable Integer id,
                                @PathVariable Integer age) {

        return "更新为id=" + id + "age=" + age;
    }


    /**
     * 删除资源
     */
    @DeleteMapping("/student/{id}")
    public String removeStudent(@PathVariable Integer id) {

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

在页面或者ajax中支持put,delete请求

在SpringMVC中有一个过滤器,支持post请求转换为put,delete请求
使用过滤器 : org.springframework.web.filter.HiddenHTTPMethodFilter
作用 : 把请求中的post转换为put和delete请求

实现步骤 :

  1. application.properties文件中开启使用HiddenHTTPMethodFilter过滤器
#启用支持put的delete
spring.mvc.hiddenmethod.filter.enabled=true
  1. 在请求的页面中需要包含_method参数,它的值是put或者delete,但是发起这个请求的是post方式
    <form action="student/test" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="submit" value="测试请求方式">
    </form>

最后在强调一点,REST风格下必须表现为唯一性,即请求方法+url是唯一的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值