接口: API(Application Programming Interface,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。
用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。
接口(API): 可以指访问servlet、controller的url, 调用其他程序的函数
架构风格: API 组织方式(样子)
就是一个传统的: http://localhost:9002/mytrans/addStudent?name=lisi&age=26
在地址上提供了 访问的资源名称addStudent
, 在其后使用了get
方式传递参数。
英文: Representational State Transfer
中文: 表现层状态转移
REST:是一种接口的架构风格和设计的理念,不是标准。
优点: 更简洁,更有层次
表现层状态转移:
表现层就是视图层, 显示资源的, 通过视图页面,jsp等等显示操作资源的结果。
状态: 资源变化
转移: 资源可以变化的。 资源能创建,new状态, 资源创建后可以查询资源, 能看到资源的内容,这个资源内容可以被修改, 修改后资源和之前的不一样。
用REST表示资源和对资源的操作。
在互联网中,表示一个资源或者一个操作。
资源是使用url表示的, 在互联网中使用的图片、视频、文本、网页等等都是资源。资源是用名词表示的。
对资源:
资源使用url表示,通过名词表示资源。
在url中使用名词表示资源以及访问资源的信息,在url中使用" / "
分隔对资源的信息 :http://localhost:8080/myboot/student/1001
使用http中的动作(请求方式)表示对资源的操作(CURD)
请求方式:
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 inserthttp://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 update<form action="http://localhost:8080/myboot/student/1" method="post">
姓名:<input type="text" name="name" />
年龄:<input type="text" name="age" />
<input type="hidden" name="_method" value="PUT" />
form>
DELETE
: 删除资源 – sql delete<a href="http://localhost:8080/myboot/student/1">删除1的数据a>
需要的分页、 排序等参数,依然放在 url的后面, 例如:http://localhost:8080/myboot/students?page=1&pageSize=20
使用url
表示资源 ,使用http
动作操作资源。
@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
组合。
在类的上面使用@RestController
, 表示当前类者的所有方法都加入了@ResponseBody
更新资源:
删除资源:
Postman : 测试工具
官网地址
使用Postman : 可以测试 get、post 、 put 、delete 等请求
post请求:
put请求:
在SpringMVC中 有一个过滤器支持post请求转为put ,delete
过滤器: org.springframework.web.filter.HiddenHttpMethodFilter
作用: 把请求中的post请求转为 put 、delete
实现步骤:
application.properties(yml)
: 开启使用 HiddenHttpMethodFilter
过滤器_method
参数, 值是 put
、delete
, 发起这个请求使用的post方式。➢ 增 post 请求、删 delete 请求、改 put 请求、查 get 请求
➢ 请求路径不要出现动词
例如:查询订单接口 /boot/order/1021/1(推荐)
/boot/queryOrder/1021/1(不推荐)
➢ 分页、排序等操作,不需要使用斜杠传参数
例如:订单列表接口 /boot/orders?page=1&sort=desc
一般传的参数不是数据库表的字段,可以不采用斜杠