接口架构风格RETSTful

奋斗吧
奋斗吧
擅长邻域:未填写

标签: 接口架构风格RETSTful Java博客 51CTO博客

2023-06-05 18:24:24 146浏览

接口架构风格RETSTful,1. 认识 REST   571.1 rest基本介绍  57REST(英文:Representational State Transfer,简称 REST) 一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原

1. 认识 REST   57

1.1 rest基本介绍  57

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

1.2 API是什么  57

接口: 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方式传递参数。

2. RESTful架构风格  58

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

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

优点: 更简洁,更有层次

2.1 表现层状态转移:   60

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

状态: 资源变化

转移: 资源可以变化的。 资源能创建,new状态,  资源创建后可以查询资源, 能看到资源的内容,

这个资源内容 ,可以被修改, 修改后资源 和之前的不一样。  

2.2 REST中的要素:  58

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

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

资源是用名词表示。

对资源: 

查询资源: 看,通过url找到资源。 

创建资源: 添加资源

更新资源:更新资源 ,编辑

删除资源: 去除

资源使用url表示,通过名词表示资源。

在url中,使用名词表示资源, 以及访问资源的信息,  在url中,使用“ / " 分隔对资源的信息

样例: http://localhost:8080/myboot/student/1001

2.3 使用http中的动作(请求方式), 表示对资源的操作(CURD)58-59

GET:  查询资源  --  sql select   59

处理单个资源: 用他的单数方式

http://localhost:8080/myboot/student/1001

  http://localhost:8080/myboot/student/1001/1

  处理多个资源:使用复数形式

http://localhost:8080/myboot/students/1001/1002

POST: 创建资源  -- sql insert  59

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   59

<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   59

<a href="http://localhost:8080/myboot/student/1">删除1的数据</a>

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

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

 一句话说明REST: 

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

3. RESTful 的注解   61

Spring Boot 开发 RESTful 主要是几个注解实现

REST注意url加上请求凡是必须唯一

3.1 @PathVariable 

获取 url 中的数据

该注解是实现 RESTFul 最主要的一个注解 

3.2   @GetMapping  62

支持的get请求方式,  等同于 @RequestMapping( method=RequestMethod.GET)

/**
     * @PathVariable(路径变量) : 获取url中的数据
     *         属性: value : 路径变量名
     *         位置: 放在控制器方法的形参前面
     *
     * http://localhost:9001/myboot/student/1001
     *
     * {stuId}:定义路径变量, stuId自定义名称
     */
    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable("stuId") Integer studentId){
        return "查询学生studentId="+studentId;
    }
    

在浏览器输入http://localhost:9001/myboot/student/1001

动态的获取到了学生得id

接口架构风格RETSTful_spring

3.3 @PostMapping   63

接收和处理 Post 方式的请求

 支持post请求方式 ,等同于 @RequestMapping( method=RequestMethod.POST)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h3>添加学生</h3>
    <form action="student/zhangsan/20" method="post">
        <input type="submit" value="注册学生">
    </form>

</body>
</html>
/***
     * 创建资源 Post请求方式  63
     * http://localhost:9001/myboot/student/zhangsan/20
     */
    @PostMapping("/student/{name}/{age}")
    public String createStudent(@PathVariable("name") String name,
                                @PathVariable("age") Integer age){
        return "创建资源 student: name="+name+"#age="+age;
    }

接口架构风格RETSTful_REST_02

3.4  @PutMapping  63

支持put请求方式,  等同于 @RequestMapping( method=RequestMethod.PUT)

当路径变量名称和 形参名一样, @PathVariable中的value可以省略

/**
     * 更新资源  63
     *
     * 当路径变量名称和 形参名一样, @PathVariable中的value可以省略
     */
    @PutMapping("/student/{id}/{age}")
    public String modifyStudent(@PathVariable Integer id,
                                @PathVariable Integer age){
        return "更新资源, 执行put请求方式: id="+id+"#age="+age;

    }

这里测试我们用Postman

接口架构风格RETSTful_html_03

3.5 @DeleteMapping   63

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

支持delete请求方式,  等同于 @RequestMapping( method=RequestMethod.DELETE)

/**
     * 删除资源  63
     */
    @DeleteMapping("/student/{id}")
    public String removeStudentById(@PathVariable Integer id){
        return "删除资源,执行delete, id="+id;
    }

接口架构风格RETSTful_spring_04

3.6 @RestController  62

复合注解, 是@Controller 和@ResponseBody组合。

在类的上面使用@RestController , 表示当前类者的所有方法都加入了 @ResponseBody

package com.bjpowernode.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

//@RestController的使用 这是复合注解, 是@Controller 和@ResponseBody组合。 62
@RestController
public class MyRestController {
    //学习注解的使用
}

4. Postman : 测试工具  63

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

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

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

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

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

5.1 实现步骤:  64

我们只演示put就行了

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

#启用支持put和delete  64
spring.mvc.hiddenmethod.filter.enabled=true

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h3>测试rest支持的请求方式  64</h3>
    <form action="student/test" method="post">
        <input type="hidden" name="_method" value="put"/>
        <input type="submit" value="测试请求方式">
    </form>

</body>
</html>
//在页面中或者ajax中,支持put,delete请求  64
    @PutMapping("/student/test")
    public String test(){
        return "执行student/test,使用请求方式put";
    }

接口架构风格RETSTful_REST_05

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695