Restful 风格请求

RESTful

  • RESTful:接口架构风格
    • 接口(API):可以指 servlet、controller 的 url、调用其他程序的函数等
  • 互联网软件架构设计的一种风格,但不是标准
    • 提出了一组客户端和服务器交互时的架构理念和设计原则
      • 基于这种理念和原则设计的接口可以更简洁、更有层次
    • 任何技术都可以实现这种理念
      • 一个架构符合 REST 原则就称为 RESTful 架构
    • 传统风格
      • 传统 url 使用 get 方式传递参数:参数使用 ? 追加在 url 之后

REST

  • REST:Representational State Transfer

    • 表现层状态转移

      • 即视图层:显示资源,通过视图页面 jsp 等显示操作资源的结果
    • 状态转移:资源的状态变化

      • 比如:创建资源、创建后可以查询看到资源内容,可以被修改,可以删除
    • 即使用 url 表示资源,使用 http 动作操作资源

  • REST 中的要素

    • 利用 REST 表示资源和对资源的操作
      • 互联网中对资源或操作使用 url 表示,
    • 资源使用名词表示
    • 使用 HTTP 中的动作(请求方式)表示对资源的操作(CRUD)
  • 架构风格:API 的组织方式

    • url 中使用名词表示资源及访问资源的信息,使用 / 分隔资源信息

      • http://localhost:8080/Demo/demo/100/18
        • 表示访问 /Demo/demo 地址资源 id = 100.age = 18 的记录
        • 自定义参数表示的含义
  • url 中的路径在访问相同数据类型变量时很可能会有歧义

    • 必须保证请求唯一
      • url 地址 + 请求方式 唯一
  • 需要的分页、排序等参数依然在 url 之中

    • http://localhost:8080/Demo/demos?page=18&pageSize=20
      • 与资源紧密相关的放在url中
  • GET:请求资源,POST:创建资源

    • 直接支持,主要使用
    <!-- 请求资源 -->
    <!-- 处理单个资源:demo 单数方式查询 id=1,age=22 的记录 -->
    http://localhost:8080/Demo/demo/1/22
    <!-- 处理多个资源:demos 复数形式查询 id=1或2 的记录 -->
    http://localhost:8080/Demo/demos/1/2
    
    <!-- 增加记录 -->
    http://localhost:8080/Demo/demo
    <!-- 在请求中传递数据 -->
    <form action="http://localhost:8080/Demo/demo" methon="post">
       姓名:<input type="text" name="name"/>
       年龄:<input type="text" age="18"/>
    </form>
    
  • PUT:更新资源,DELETE:删除资源;默认直接不支持

    <!-- 更新资源 -->
    http://localhost:8080/Demo/demo
    <!-- 在请求中传递数据 -->
    <form action="http://localhost:8080/Demo/demo" methon="post">
       姓名:<input type="text" name="name"/>
       年龄:<input type="text" age="18"/>
        <!-- 声明使用 put 请求处理资源 -->
        <input type="hidden" name="_method" value="PUT">
    </form>
    
    <!-- 删除资源:自定义接收对应请求与参数类型的处理方式 -->
    <a href="http://localhost:8080/Demo/demo/1">删除记录id=1</a>
    

注解

  • Spring 开发 RESTful 主要是几个注解实现
@RestController
  • @Controller@ResponseBody 的复合注解

  • 在类定义使用 @RestController

    • 表示当前类所有方法都添加了 @ResponseBody 注解
    • 使用等价于 @Controller
@PathVariable
  • 获取 url 中的数据

    • 实现 RESTful 最主要的注解
  • 注解在方法形参前

    • 请求 url 中使用占位符 {变量名} 表示参数
    • 注解属性 value = "变量名" 将路径变量赋值给被注解的形参
      • 路径变量名和形参名一致时可省略 value
    // RestFul 风格参数接收
    @GetMapping("/demo/{name}/{age}")
    public void test(@PathVariable(value = "name") String name, 
                     @PathVariable(value = "age") Integer age){}
    
请求接收
  1. @PostMapping

    • 接收和处理 post 请求的方式

    • 等价于 @RequestMapping(method=RequestMethod.POST)

  2. @GetMapping

    • 支持的 get 请求方式

    • 等价于 @RequestMapping(method=RequestMethod.GET)

  3. @DeleteMapping

    • 接收 delete 方式的请求,可使用 GetMapping 代替
  4. @PutMapping

    • 支持 put 请求方式
controller 类示例
package springboot_mybatis.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot_mybatis.service.DemoService;

import javax.annotation.Resource;
//注解表示此类是 controller,且所有方法添加 @ResponseBody 返回数据对象
@RestController
public class MyController {

    @Resource
    private DemoService demoService;

    // 请求方式 + url 必须保证唯一以准确获取资源
    
    /**
     * 查询记录
     * @GetMapping get方式处理请求
     * {id}:url 路径中的变量
     * url相当于 http://localhost:8080/doGet/100
     * @PathVariable 将路径变量赋值到形参 id
     * @param id 记录 id
     * @return 返回结果数据
     */
    @GetMapping("/doGet/{id}")
    public String doGet(@PathVariable("id") Integer id){
        return demoService.getById(100).toString();
    }

    /**
     * post 请求添加记录
     * url相当于 http://localhost:8080/doPost/{name}/{age}
     * @param age age
     * @param name name
     * @return 返回结果数据
     */
    @PostMapping("/doPost/{name}/{age}")
    public String doPost(@PathVariable("name") String name,
                         @PathVariable("age") Integer age){
        return "添加记录 name = " + name + ";age = " + age;
    }

}
put、delete
  • 在页面或 Ajax 中支持 put、delete 请求
    • 默认页面不支持 put、delete
  • SpringMVC 中有过滤器支持将 post 转为 put、delete
    • SpringMVC 中需要在 web.xml 文件配置

实现

  1. application.properties 配置文件中开启使用 HiddenHttpMethodFilter 过滤器
  2. 在请求页面中包含 _method 参数,值为 put 或 delete
    • 发起请求使用 post 方法
# 启用支持 put、delete
spring.mvc.hiddenmethod.filter.enabled=true
<body>
    <form actoin="demo/test" method="post">
        <!-- 隐藏域,声明使用 put 请求 -->
        <input type="hidden" name="_method" value="PUT"/>
        <input type="submot" value="测试put请求"/>
	</form>
    <form actoin="demo/test" method="post">
        <!-- 隐藏域,声明使用 delete 请求 -->
        <input type="hidden" name="_method" value="delete"/>
        <input type="submot" value="测试put请求"/>
    </form>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值