当前位置:网站首页>9. Rest style request processing
9. Rest style request processing
2022-08-10 23:48:00 【If you want to learn, learn Big Wolf】
文章目录
9.1 基本介绍
Rest 风格支持 (使用 HTTP 请求方式动词来表示对资源的操作)
举例说明:
● 请求方式: /monster
● GET - Get the little monster
● DELETE - Delete the little monster
● PUT - Modify the little monster
● POST - Save the little monster
9.2 SpringBoot Rest Example of style application
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-Inquire about little monsters";
}
//@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-Modify the little monster";
}
//@RequestMapping(value = "/monster", method = RequestMethod.DELETE)
@DeleteMapping("/monster")
public String delMonster() {
return "DELETE-Delete the little monster";
}
}
- 使用 Postman 完成测试,请求 url: http://localhost:8080/monster

9.2.3 Rest 风格请求 -注意事项和细节
客户端是 PostMan 可以直接发送 Put、delete 等方式请求,可不设置 Filter
如果要 SpringBoot 支持 page form Rest 功能,You need to pay attention to the following details
① Rest Style request core Filter — HiddenHttpMethodFilter
② The form request will be HiddenHttpMethodFilter 拦截,Get the form _method 的值, 再判断是 PUT/DELETE/PATCH
③ 说明:PATCH 方法是新引入的,是对 PUT 方法的补充,用来对已知资源进行局部更新
④ https://segmentfault.com/q/1010000005685904如果要 SpringBoot 支持 page form Rest 功能,需要在 application.yml 启用 filter 功能,否则无效
修改 application.yml 启用 filter 功能

spring:
mvc:
static-path-pattern: /xjsres/** #Modify the path of static resource access/前缀
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter 开启page form rest 功能
web:
resources:
#修改/指定 静态资源的 存放路径/位置 # String[] staticLocations
#注意:配置了 static-locations The original access path is overwritten,如果需要保留,To specify again
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-查询妖怪",返回的是字符串,rather than forwarding to 对
corresponding resource file?
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-查询妖怪" ,返回的是字符串,rather than forwarding to 对 * corresponding resource file? * * 解读: * 1. 因为@ResController 是一个复合注解, 含有@ResponseBody * 2. 所以 springboot 底层(springmvc), 在处理 return "xxx" 时, * 会以@ResponseBody 注解进行解析处理, 即返回字符串 "xxx", without using the view resolver * 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-Inquire about little monsters";
}
//@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-Modify the little monster";
}
//@RequestMapping(value = "/monster", method = RequestMethod.DELETE)
@DeleteMapping("/monster")
public String delMonster() {
return "DELETE-Delete the little monster";
}
@RequestMapping("/go")
public String go() {
return "hello";
//注意:
// 1.看controller 有没有 /hello [The premise is that no view resolver is configured]
// 2.If a view resolver is configured, the page is located according to the view resolver
}
}
---------- 在 application.yml 配置解析器 ----------

spring:
mvc:
static-path-pattern: /xjsres/** #Modify the path of static resource access/前缀
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter 支持 rest风格请求
view: # 配置了视图解析器
suffix: .html
prefix: /xjsres/ # 这里需要注意 前缀prefix 要考虑 static-path-pattern 的配置
web:
resources:
#修改/指定 静态资源的 存放路径/位置 # String[] staticLocations
#注意:配置了 static-locations The original access path is overwritten,如果需要保留,To specify again
static-locations: ["classpath:/xjsimg/", "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"]
- Test possible problems and solution analysis

- 如何解决

边栏推荐
- Kubernetes 维护技术分享
- sqlmap结合dnslog快速注入
- 编程语言为什么有变量类型这个概念?
- How to quickly grasp industry opportunities and introduce new ones more efficiently is an important proposition
- Talking about jsfuck coding
- HGAME 2022 Week4 writeup
- ROS实验笔记之——安装QPEP以及Intel-MKL
- 翻译软件哪个准确度高【免费】
- DASCTF X SU 2022 writeup
- 电脑桌面删除的文件回收站没有,电脑上桌面删除文件在回收站找不到怎么办
猜你喜欢

关于弱监督学习的详细介绍——A Brief Introduction to Weakly Supervised Learning

Geogebra 教程之 02 Geogebra初学者的 8 个基本要素
![[C language] binary search (half search)](/img/24/4e7b54963ac9df4a3244232c32c435.png)
[C language] binary search (half search)

【C语言】C语言程序设计:动态通讯录(顺序表实现)

C194铜合金C19400铁铜合金

花环灯问题

C language, operators of shift operators (> >, < <) explanation

Multilingual Translation - Multilingual Translation Software Free

HPb59-1铅黄铜

回收站的文件删了怎么恢复,回收站文件恢复的两种方法
随机推荐
Doris建表注意事项,实时数仓的同学记得收藏
mysql数据库高级操作
一条SQL查询语句是如何执行的?
【C语言】二分查找(折半查找)
Multilingual Translation - Multilingual Translation Software Free
回收站的文件删了怎么恢复,回收站文件恢复的两种方法
Kubernetes你不知道的事
性能不够,机器来凑;jvm调优实战操作详解
逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
使用PageHelper自定义PageInfo进行分页+模糊查询
正交基(线性代数)
How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
7. yaml
22年全国程序员1月薪资出炉,才知道年薪 40 万以上的有这么多?
Mathematical modeling preparation knowledge
2. 依赖管理和自动配置
N1BOOK writeup
Rust从入门到精通05-语句和表达式
HGAME 2022 Week4 writeup
sqlmap结合dnslog快速注入