接口架构风格 —RESTful

目录

接口架构风格 —RESTful

REST

1)REST :  

2)REST中的要素:

3) 一句话说明REST:

4) 注解

5) Postman : 测试工具

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


接口架构风格 —RESTful

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

接口(API): 可以指访问servlet, controller的url,调用其他程序的函数

架构风格: api组织方式

   就是一个传统的:    http://localhost:9002/mytrans/addStudent?name=lisi&age=26 

在地址上提供了 访问的资源名称addStudent, 在其后使用了get方式传递参数。

REST

RESTful架构风格

1)REST :  

(英文: Representational State Transfer , 中文: 表现层状态转移)。

   REST:是一种接口的架构风格和设计的理念,不是标准。

   优点: 更简洁,更有层次

   表现层状态转移: 

​         表现层就是视图层, 显示资源的, 通过视图页面,jsp等等显示操作资源的结果。

​         状态: 资源变化

​         转移: 资源可以变化的。 资源能创建,new状态,  资源创建后可以查询资源, 能看到资源的内容,这个资源内容 ,可以被修改, 修改后资源 和之前的不一样。  

2)REST中的要素:

   用REST表示资源和对资源的操作。  在互联网中,表示一个资源或者一个操作。 

   资源使用url表示的, 在互联网, 使用的图片,视频, 文本,网页等等都是资源。

   资源是用名词表示。

  对资源: 

​        查询资源: 看,通过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 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 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

```xml

删除1的数据 ```

需要的分页, 排序等参数,依然放在 url的后面, 例如

http://localhost:8080/myboot/students?page=1&pageSize=20

`

3) 一句话说明REST:

使用url表示资源 ,使用http动作操作资源。

4) 注解

@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

路径变量Pathvariable

在右上角新建一个模块(图省略了) 

然后一直next就行!

编写 Controller
创建 MyRestController
package com.bjpowernode.controller;
import org.springframework.web.bind.annotation.*;
www.bjpowernode.com 62 / 137 Copyright©动力节点@RestController
public class MyRestController {
//get 请求
/**
* rest 中, url 要使用占位符,表示传递的数据。
* 占位符叫做路径变量, 在 url 中的数据
*
* 格式: 在@RequestMapping 的 value 属性值中,使用 {自定义名称}
* http://localhost:8080/myboot/student/1001/bj2009
*
*
* @PathVariable: 路径变量注解,作用是获取 url 中的路径变量的值
* 属性: value : 路径变量名称
* 位置: 在逐个接收参数中,在形参定义的前面
*
* 注意:路径变量名和形参名一样, value 可以不写
*
* /student/1001/bj2009
*/
@GetMapping(value = "/student/{studentId}/{classname}")
public String queryStudent(@PathVariable(value = "studentId") Integer
id,
@PathVariable String classname){
return "get 请求,查询学生 studentId:"+id+", 班级:"+classname;
}
///student/1001/bjpowernode
@GetMapping(value = "/student/{studentId}/school/{schoolname}")
public String queryStudentBySchool(@PathVariable(value = "studentId")
Integer id,
@PathVariable String schoolname){
return "get 请求,查询学生 studentId:"+id+", 班级:"+schoolname;
}
@PostMapping("/student/{stuId}")
public String createStudent(@PathVariable("stuId") Integer id,
String name,
Integer age){
return "post 创建学生, id="+id+",name="+name+",age="+age;
}
@PutMapping("/student/{stuId}")
public String modifyStudent(@PathVariable("stuId") Integer id,
String name){
System.out.println("===========put 请求方式 ========");
return "put 修改学生, id="+id+",修改的名称是:"+name;
}
@DeleteMapping("/student/{stuId}")
public String removeStudent(@PathVariable("stuId") Integer id){
System.out.println("===========delete 请求方式 ========");
return "delete 删除学生,id="+id;
}
}
application.properties 文件
server.servlet.context-path=/myboot
启用 HiddenHttpMethodFilter 这个过滤器, 支持 post 请求转为 put,delete
spring.mvc.hiddenmethod.filter.enabled=true

5) Postman : 测试工具

使用Postman : 可以测试 get ,post , put ,delete 等请求

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

在SpringMVC中 有一个过滤器, 支持post请求转为put ,delete

过滤器: org.springframework.web.filter.HiddenHttpMethodFilter

作用: 把请求中的post请求转为 put , delete

实现步骤:

  1. application.properties(yml) : 开启使用 HiddenHttpMethodFilter 过滤器

  2. 在请求页面中,包含 _method参数, 他的值是 put, delete , 发起这个请求使用的post方式

 

想了解API接口,这一篇就够了_AI_孟菜菜的博客-CSDN博客_api接口

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值