目录
获取请求参数常用的注解有5个:
- RequestParam注解
- RequestBody注解
- RequestHeader注解
- CookieValue注解
- PathVaribale注解
1. RequestParam注解
作用
用于将请求参数绑定到控制器方法的参数上,主要处理URL中的查询参数或表单数据。
也就是说,把请求中的指定名称的参数传递给控制器中的形参赋值。
属性
- value/name:请求参数中的名称
- required:请求参数中是否必须提高此参数,默认是true,必须提供
- defaultValue:指定默认的值(如果没有传请求参数,使用默认指定的值)
使用场景
- 获取URL查询参数:?name=value&key=value
- 获取表单提交的字段值:application/x-www-form-urlencoded
- 分页查询参数
- 搜索过滤条件
操作案例
- jsp页面
<h3>RequestParam注解</h3>
<form method="post" action="/dept/dept.do">
姓名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="提交"><br/>
</form>
- Controller代码
package com.qcby.demo2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/dept")
@Controller
public class DeptController {
/**
* RequestParam注解
* value:请求参数名称
* required = false:默认值是true,必须要传请求参数,不传就会报错
* defaultValue = "abc":如果没有传请求参数,使用默认指定的值abc
* @param name
* @return
*/
@RequestMapping("/dept.do")
public String save(@RequestParam(value = "username",required = false,defaultValue = "abc")String name){
System.out.println("姓名:"+name);
return "suc";
}
}
- 输出结果
姓名:张三

2. RequestBody注解
作用
用于将HTTP请求体中的JSON/XML数据绑定到Java对象上,主要处理POST/PUT请求的请求体数据(get请求不可以)。
属性
- required:是否必须有请求体,默认值是true
使用场景
- Restful API的创建、更新操作
- 接受前端提交的JSON/XML数据
- 批量数据处理
- 前端通过Ajax发送的复杂对象的数据绑定
操作案例
- jsp页面
<h3>RequestBody注解</h3>
<form method="post" action="/dept/body.do">
姓名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="提交"><br/>
</form>
- Controller代码
/**
* @RequestBody注解:用于post/put请求方法
* 什么请求需要使用,什么情况下不需要
* @param body
* @return
*/
@RequestMapping("/body.do")
public String save2(@RequestBody String body){
System.out.println("请求体内容:"+body);
return "suc";
}
- 输出结果
请求体内容:username=%E8%B5%B5%E5%85%AD&age=21

3. RequestHeader注解
作用
用于将HTTP请求头信息绑定到控制器方法的参数上。
属性
- value/name:请求头名称
- required:是否必须,默认true
- defaultValue:默认值
使用场景
- 获取认证信息(Authorization头)
- 获取客户端信息(User-Agent)
- 处理多语言(Accept-Language)
- 自定义业务头信息处理
- API版本控制
操作案例
- jsp页面
<h3>RequestHeader注解</h3>
<form method="post" action="/dept/header.do">
姓名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="提交"><br/>
</form>
- Controller代码
/**
* RequestHeader注解
* @param header
* @return
*/
@RequestMapping("/header.do")
public String save3(@RequestHeader(value = "Accept") String header){
System.out.println("Accept请求头的值:"+header);
return "suc";
}
- 输出结果
Accept请求头的值:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

4. CookieValue注解
作用
用于获取指定cookie的名称的值。
属性
- value/name:Cookie名称
- required:是否必须,默认true
- defaultValue:默认值
使用场景
- 会话管理(JSESSIONID)
- 用户偏好设置(主题、语言)
- 跟踪用户行为
- 记住登录状态
操作案例
- jsp页面
<h3>CookieValue注解</h3>
<form method="post" action="/dept/cookie.do">
姓名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="提交"><br/>
</form>
- Controller代码
/**
* CookieValue注解
* @param cookie
* @return
*/
@RequestMapping("/cookie.do")
public String save4(@CookieValue(value = "JSESSIONID") String cookie){
System.out.println("值:"+cookie);
return "suc";
}
- 输出结果
值:0B73C7544168540B92D6592C2147F57C


5. PathVaribale注解
作用
用于将URL模板变量绑定到控制器方法的参数上,主要用于RESTful风格的URL。
拥有绑定url中的占位符的。例如:url中有/delete/{id},{id}就是占位符
属性
- value:指定url中的占位符名称
使用场景
- RESTful API的资源标识
- 层级资源访问
- 语义化URL
- SEO友好的URL结构
Restful风格的URL
- 请求路径一样,可以根据不同的请求方式去执行后台的不同方法
- restful风格的URL优点
- 结构清晰
- 符合标准
- 易于理解
- 扩展方便
操作案例
- jsp页面
<h3>@PathVariable注解</h3> <form method="post" action="/emp"> <input type="submit" value="提交 - post请求方式"><br/> </form> <h3>@PathVariable注解2</h3> <a href="/emp">get请求方式</a> <h3>@PathVariable注解3</h3> <form method="get" action="/emp/1"> <input type="submit" value="提交 - 通过占位符查找"><br/> </form> - Controller代码
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class EmpController { /** * 保存 * @return */ @RequestMapping(path = "/emp",method = RequestMethod.POST) public String save(){ System.out.println("保存员工……"); return "suc"; } /** * 查询所有 * @return */ @RequestMapping(path = "/emp",method = RequestMethod.GET) public String findAll(){ System.out.println("查询员工……"); return "suc"; } /** * 通过id查询 * @param id * @return */ @RequestMapping(path = "/emp/{id}",method = RequestMethod.GET) public String findById(@PathVariable(value = "id")Integer id){ System.out.println("通过id查询员工..."+id); return "suc"; } } - 输出结果
保存员工…… 查询员工…… 通过id查询员工...1
413

被折叠的 条评论
为什么被折叠?



