当前位置:网站首页>9. Rest 风格请求处理
9. Rest 风格请求处理
2022-08-10 23:35:00 【要学就学灰太狼】
9.1 基本介绍
Rest 风格支持 (使用 HTTP 请求方式动词来表示对资源的操作)
举例说明:
● 请求方式: /monster
● GET - 获取小妖怪
● DELETE - 删除小妖怪
● PUT - 修改小妖怪
● POST - 保存小妖怪
9.2 SpringBoot Rest 风格应用实例
9.2.1 需求说明
- 演示 SpringBoot 中如何实现 Rest 风格的 增删改查
9.2.2 应用实例
- 创建 D:\xjs_springboot\springbootweb\src\main\java\com\xjs\springboot\controller\MonsterController.java
package com.xjs.springboot.controller;
import org.springframework.web.bind.annotation.*;
/** * @Author: 谢家升 * @Version: 1.0 */
@RestController
public class MonsterController {
//@RequestMapping(value = "/monster", method = RequestMethod.GET)
@GetMapping("/monster")
public String getMonster() {
return "GET-查询小妖怪";
}
//@RequestMapping(value = "/monster", method = RequestMethod.POST)
@PostMapping("/monster")
public String saveMonster() {
return "POST-添加小妖怪";
}
//@RequestMapping(value = "/monster", method = RequestMethod.PUT)
@PutMapping("/monster")
public String putMonster() {
return "PUT-修改小妖怪";
}
//@RequestMapping(value = "/monster", method = RequestMethod.DELETE)
@DeleteMapping("/monster")
public String delMonster() {
return "DELETE-删除小妖怪";
}
}
- 使用 Postman 完成测试,请求 url: http://localhost:8080/monster
9.2.3 Rest 风格请求 -注意事项和细节
客户端是 PostMan 可以直接发送 Put、delete 等方式请求,可不设置 Filter
如果要 SpringBoot 支持 页面表单的 Rest 功能,则需要注意如下细节
① Rest 风格请求核心 Filter — HiddenHttpMethodFilter
② 表单请求会被 HiddenHttpMethodFilter 拦截,获取到表单 _method 的值, 再判断是 PUT/DELETE/PATCH
③ 说明:PATCH 方法是新引入的,是对 PUT 方法的补充,用来对已知资源进行局部更新
④ https://segmentfault.com/q/1010000005685904如果要 SpringBoot 支持 页面表单的 Rest 功能,需要在 application.yml 启用 filter 功能,否则无效
修改 application.yml 启用 filter 功能
spring:
mvc:
static-path-pattern: /xjsres/** #修改静态资源访问的路径/前缀
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter 开启页面表单的 rest 功能
web:
resources:
#修改/指定 静态资源的 存放路径/位置 # String[] staticLocations
#注意:配置了 static-locations 原来的访问路径就被覆盖了,如果需要保留,要再指定一下
static-locations: ["classpath:/xjsimg/", "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"]
- 创建测试页面 D:\xjs_springboot\springbootweb\src\main\resources\public\rest.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rest</title>
</head>
<body>
<h1>测试rest风格的url, 来完成请求</h1>
<form action="/monster" method="post">
name: <input type="text" name="name"><br/>
<!--通过隐藏域传递 _method 参数指定值-->
<input type="hidden" name="_method" value="put">
<input type="submit" value="点击提交">
</form>
</body>
</html>
- 完成测试,注意 url 是 localhost:8080/xjsres/rest.html,如果希望 url 是
localhost:8080/rest.html,将 application.yml 中的static-path-pattern: /xjsres/**
注销即可
9.3 思考题
- 为什么这里
return "GET-查询妖怪"
,返回的是字符串,而不是转发到 对
应的资源文件?
package com.xjs.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/** * @Author: 谢家升 * @Version: 1.0 */
//@RestController
@Controller
public class MonsterController {
/** * 思考题:为什么这里 return "GET-查询妖怪" ,返回的是字符串,而不是转发到 对 * 应的资源文件? * * 解读: * 1. 因为@ResController 是一个复合注解, 含有@ResponseBody * 2. 所以 springboot 底层(springmvc), 在处理 return "xxx" 时, * 会以@ResponseBody 注解进行解析处理, 即返回字符串 "xxx", 而不会使用视图解析器来处理 * 3. 可以试一下, 如果我们把 @RestController 改成 @Controller , 当你访问 * getMonster() 时, 如果你有 xxx.html 会转发到 xxx.html , 如果没有 xxx.html , 就会报 404 * 4. 提示: 在测试时, 将 xxx.html 放在 main\resources\public\xxx.html 进行测试, 并在 * application.yml 配置视图解析器 */
//@RequestMapping(value = "/monster", method = RequestMethod.GET)
@GetMapping("/monster")
public String getMonster() {
return "GET-查询小妖怪";
}
//@RequestMapping(value = "/monster", method = RequestMethod.POST)
@PostMapping("/monster")
public String saveMonster() {
return "POST-添加小妖怪";
}
//@RequestMapping(value = "/monster", method = RequestMethod.PUT)
@PutMapping("/monster")
public String putMonster() {
return "PUT-修改小妖怪";
}
//@RequestMapping(value = "/monster", method = RequestMethod.DELETE)
@DeleteMapping("/monster")
public String delMonster() {
return "DELETE-删除小妖怪";
}
@RequestMapping("/go")
public String go() {
return "hello";
//注意:
// 1.看controller 有没有 /hello [前提是没有配置视图解析器]
// 2.如果配置了视图解析器就按照视图解析器来定位页面
}
}
---------- 在 application.yml 配置解析器 ----------
spring:
mvc:
static-path-pattern: /xjsres/** #修改静态资源访问的路径/前缀
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter 支持 rest风格请求
view: # 配置了视图解析器
suffix: .html
prefix: /xjsres/ # 这里需要注意 前缀prefix 要考虑 static-path-pattern 的配置
web:
resources:
#修改/指定 静态资源的 存放路径/位置 # String[] staticLocations
#注意:配置了 static-locations 原来的访问路径就被覆盖了,如果需要保留,要再指定一下
static-locations: ["classpath:/xjsimg/", "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"]
- 测试可能出现的问题和解决方案分析
- 如何解决
边栏推荐
- 如果纯做业务测试的话,在测试行业有出路吗?
- 逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
- CW617N锡青铜 CuZn40Pb2对应牌号
- How to quickly grasp industry opportunities and introduce new ones more efficiently is an important proposition
- HGAME 2022 Week2 writeup
- DASCTF X SU 2022 writeup
- Rust从入门到精通05-语句和表达式
- 【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
- 回收站的文件删了怎么恢复,回收站文件恢复的两种方法
- 开源一夏|OpenHarmony如何选择图片在Image组件上显示(eTS)
猜你喜欢
随机推荐
CW614N铜棒CuZn39Pb3对应牌号
[C] the C language program design, dynamic address book (order)
ROS Experimental Notes - Install QPEP and Intel-MKL
[C language] Implementation of guessing number game
22年全国程序员1月薪资出炉,才知道年薪 40 万以上的有这么多?
Android面试冲刺:2022全新面试题——剑指Offer(备战金九银十)
6.0深入理解MySQL事务隔离级别与锁机制
excel英文自动翻译成中文教程
Redis - Use lua script to control the number of wrong passwords and lock the account
Easy-to-use translation plug-in - one-click automatic translation plug-in software
花环灯问题
定时器,同步API和异步API,文件系统模块,文件流
Kioptrix Level 1 靶机wp
李宏毅机器学习-- Backpropagation
CSDN21天学习挑战赛之折半查找
Mathematical modeling preparation knowledge
互联网中不可缺失的网络优化:“App与Server的交互依赖于网络”
u盘数据不小心删除怎么恢复,u盘数据删除如何恢复
CSAPP lab0 实验环境搭建
Unity--URP渲染管线实战教程系列之URP摄像机核心机制剖析