RestFul风格

首先需要简单介绍一下,Restful风格是关于什么的

Restful风格是API(应用程序接口--访问路径如Controller/Servlet/url)的组织方式/格式

【资源】

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

在url中使用名词表示资源,以及访问资源的信息

处理多个资源时候把资源改为复数形式(如:student->students)

如:

1.经典模式:https://image.baidu.com/search/student?name=wuhao&id=1

2.RestFul风格:https://image.baidu.com/search/student/name=wuhao/id=1

四种请求的方式:

< Get: 查询资源> sql select

https://image.baidu.com/search/index/name=wuhao/id=1

< Post: 创建资源> sql create

在form表单里面通过action="https://image.baidu.com/search/index" method="post"

数据不能跟在资源后面,而是通过表单形式提交

sql update

我们浏览器不支持直接的Put请求所以还是通过Post请求转换

由于是更新资源所以需要绑定一个参数指定操作对象

<from action="https://image.baidu.com/search/index/1" method="post"> <input type="text" name="name"> <input type="text" id="id"> <input type="hidden" name="_method" value="PUT"> </form>

sql delete

依旧需要提供操作对象的数据

<a href="https://image.baidu.com/search/index/1">删除1的数据</a>

【重点注解】

@PathVariable从url中获取参数

注意,当路径变量名和形参变量名一致时,@PathVariable里面可以不用写属性值

@GetMapping 接收和处理Get方式请求

@PostMapping 接收和处理Post方式请求

@PutMapping 接收和处理Put方式请求

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

@RestController:符合注解,是@Controller和@ResponseBody组合

在类上面使用,表示该类里面的所有方法都加了@ResponseBody

【过滤器】

这对于普通请求来说,譬如from表单,正常可以设置Get,Post请求设置在

<from action="https://image.baidu.com/search/index/1" method="post">

然而对于Put和Delete来说呢,这么设置会导致路径出错,不支持这么使用,所以需要在SpringBoot里面开启过滤器来把Post转化成Put和Delete请求

具体步骤:

  1. 由于SpringBoot里面已经含有这个过滤器所以不需要我们导入,只需要启动这个过滤器即可;
  2. 在application.properties(yml)配置文件里面开启使用HiddenHttpMethodFilter过滤器
  3. 在请求页面中,包含 _method 参数,它的值是Put或者Delete,发起这个请求使用的还是Post方式
  4. 具体操作方式(开启)

mvc: hiddenmethod: filter: enabled: true

5. 页面的使用方式

<from action="student/test" method="post"> <input type="text" name="name"> <input type="text" id="id"> <input type="hidden" name="_method" value="put"> </form>

6. Controller层的书写格式

@PutMapping("student/test")

【唯一性】

即请求方式+url一定不能产生歧义性即一定是唯一的接口路径

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值