springboot接收参数详解
请求路径带参数
@PathVariable
:当请求路径格式为http://ip:port/projectName/aaaaa/{xx}
,在方法的@RequestMapping
注解里面写上@RequestMapping("user/{id}")
,表示的是
请求的最后一个字符就是id的值,在方法参数里我们用@PathVariable
注解取值。
//1、@PathVariable,前台请求格式为get: http://localhost:8088/moyundong/sysUser/user/10086
@RequestMapping("user/{id}")
public String selectById(@PathVariable(name = "id") String id){
System.out.println("参数id="+id);
return "selectById请求成功";
}
在浏览器输入http://localhost:8088/moyundong/sysUser/user/10086
就能看到结果
@RequestParam
:当请求路径格式为http://ip:port/projectName/aaaaa?id=10086
,时,在方法参数里我们用@RequestParam
注解取值。
//2、@RequestParam,前台请求格式为get: http://localhost:8088/moyundong/sysUser/selectById?id=10086
@RequestMapping("selectById")
public String selectById2(@RequestParam(name = "id") String id){
System.out.println("参数id="+id);
return "selectById2请求成功";
}
在浏览器输入http://localhost:8088/moyundong/sysUser/selectById?id=10086
就能看到结果
@PathVariable
+@RequestParam
组合使用
//3、@PathVariable + @RequestParam,前台请求格式为get: http://localhost:8088/moyundong/sysUser/selectUser/10086?name=移动客服
@RequestMapping("selectUser/{id}")
public String selectByIdAndName(@PathVariable(name = "id") String id,@RequestParam(name = "name") String name){
System.out.println("参数id="+id);
System.out.println("参数name="+name);
return "selectByIdAndName请求成功";
}
在浏览器输入http://localhost:8088/moyundong/sysUser/selectUser/10086?name=移动客服
- 不加任何注解的时候,如果用
?
传值,直接在方法参数里使用相同变量名接收就行。
//不加注解,前台请求格式为get: http://localhost:8088/moyundong/sysUser/selectUser2?id=10086&name=移动客服
@RequestMapping("selectUser2")
public String selectByIdAndName2(String id, String name){
System.out.println("参数id="+id);
System.out.println("参数name="+name);
return "selectByIdAndName2请求成功";
}
对象参数
先定义一个实体对象类SysUser
@Data
public class SysUser {
private String id;
private String username;
private String password;
private Date birthday;
private String email;
}
- post请求,参数格式是json,使用
@RequestBody
注解然后直接用对象接收参数,这种前台一般是ajax请求
@PostMapping(path = "/addUser")
public String addUser(@RequestBody SysUser sysUser) {
System.out.println(sysUser.toString());
return "addUser请求成功";
}
页面请求url:http://localhost:8088/moyundong/sysUser/addUser
参数:{"id":"10086","password":"123456","birthday":"2019-09-09","username":"admin","email":"aa@aa.com"}
2. post请求,参数格式是表单数据,在postman里面就是form-data格式
//参数是表单格式在postman里面就是form-data格式,这个里面日期类型传2019/08/08,与json格式不一样,json格式日期要传为2019-08-08格式。
@PostMapping(path = "/addUser2")
public String addUser2(SysUser sysUser) {
System.out.println(sysUser.toString());
return "addUser2请求成功";
}
页面请求url:http://localhost:8088/moyundong/sysUser/addUser2
参数:{"id":"10086","password":"123456","birthday":"2019-09-09","username":"admin","email":"aa@aa.com"}
::: warning 注意
json
格式传递日期类型的时候使用YYYY-MM-DD
,例如2019-09-09
- 表单(
form-data
格式)传递日期类型的时候使用YYYY/MM/DD
,例如2019/08/08
- 这个是我们没有使用formate的情况,使用formate的话就根据自定义类型传递就可以了。
:::
header以及Cookie
使用@RequestHeader
和@CookieValue
获取请求头和cookie的信息
@GetMapping("getCookieAndHeader")
public String getCookieAndHeader(@RequestHeader(name = "myHeader") String myHeader,
@CookieValue(name = "myCookie") String myCookie) {
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
return "getCookieAndHeader请求成功";
}
HttpServletRequest接收参数
- 通过
request
获取header
和cookie
//1 通过request获取header和cookie
@GetMapping("/getCookieAndHeader2")
public String getCookieAndHeader2(HttpServletRequest request) {
System.out.println("myHeader=" + request.getHeader("myHeader"));
for (Cookie cookie : request.getCookies()) {
if ("myCookie".equals(cookie.getName())) {
System.out.println("myCookie=" + cookie.getValue());
}
}
return "getCookieAndHeader2请求成功";
}
- 通过request获取普通参数,post和get方法都可以获取到
// http://localhost:8088/moyundong/sysUser/addUser3?username=water&password=123456
@RequestMapping("/addUser3")
public String addUser3(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username = "+username);
System.out.println("password = "+password);
return "addUser3请求成功";
}
::: tip 提示
理论上所有参数都是可以通过request获取,只不过使用不同的方法罢了。
:::
- 测试的controller贴到下面
package com.moyundong.controller;
import com.moyundong.entity.SysUser;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
/**
* @Author: www.moyundong.com
* @Date:2020-06-13 00:56:37
* @Description: TODO
**/
@RestController
@RequestMapping("sysUser")
public class SysUserController {
//第一类:请求路径参数
//1、@PathVariable,前台请求格式为get: http://localhost:8088/moyundong/sysUser/user/10086
@RequestMapping("user/{id}")
public String selectById(@PathVariable(name = "id") String id){
System.out.println("参数id="+id);
return "selectById请求成功";
}
//2、@RequestParam,前台请求格式为get: http://localhost:8088/moyundong/sysUser/selectById?id=10086
@RequestMapping("selectById")
public String selectById2(@RequestParam(name = "id") String id){
System.out.println("参数id="+id);
return "selectById2请求成功";
}
//3、@PathVariable + @RequestParam,前台请求格式为get: http://localhost:8088/moyundong/sysUser/selectUser/10086?name=移动客服
@RequestMapping("selectUser/{id}")
public String selectByIdAndName(@PathVariable(name = "id") String id,@RequestParam(name = "name") String name){
System.out.println("参数id="+id);
System.out.println("参数name="+name);
return "selectByIdAndName请求成功";
}
//不加注解,前台请求格式为get: http://localhost:8088/moyundong/sysUser/selectUser2?id=10086&name=移动客服
@RequestMapping("selectUser2")
public String selectByIdAndName2(String id, String name){
System.out.println("参数id="+id);
System.out.println("参数name="+name);
return "selectByIdAndName2请求成功";
}
//**********第二类:对象参数*****************
//1、@RequestBody,前台是post请求,参数格式是json
//http://localhost:8088/moyundong/sysUser/addUser
//{"id":"10086","password":"123456","birthday":"2019-09-09","username":"admin","email":"aa@aa.com"}
@PostMapping(path = "/addUser")
public String addUser(@RequestBody SysUser sysUser) {
System.out.println(sysUser.toString());
return "addUser请求成功";
}
//2、无参数前台是post请求,参数格式是表单
//http://localhost:8088/moyundong/sysUser/addUser2
//参数是表单格式在postman里面就是form-data格式,这个里面日期类型传2019/08/08,与json格式不一样,json格式日期要传为2019-08-08格式。
@PostMapping(path = "/addUser2")
public String addUser2(SysUser sysUser) {
System.out.println(sysUser.toString());
return "addUser2请求成功";
}
// 第三类:请求头参数以及Cookie
@GetMapping("getCookieAndHeader")
public String getCookieAndHeader(@RequestHeader(name = "myHeader") String myHeader,
@CookieValue(name = "myCookie") String myCookie) {
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
return "getCookieAndHeader请求成功";
}
// 第四类:HttpServletRequest接收
//1 通过request获取header和cookie
@GetMapping("/getCookieAndHeader2")
public String getCookieAndHeader2(HttpServletRequest request) {
System.out.println("myHeader=" + request.getHeader("myHeader"));
for (Cookie cookie : request.getCookies()) {
if ("myCookie".equals(cookie.getName())) {
System.out.println("myCookie=" + cookie.getValue());
}
}
return "getCookieAndHeader2请求成功";
}
//2 通过request获取普通参数,post和get方法都可以获取到
// http://localhost:8088/moyundong/sysUser/addUser3?username=water&password=123456
@RequestMapping("/addUser3")
public String addUser3(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username = "+username);
System.out.println("password = "+password);
return "addUser3请求成功";
}
}
本节示例下载地址:java相关demo下载列表
1介绍
2springboot定时任务
3springboot定时任务配置详解
4springboot动态定时任务
5springboot集成websocket
6springboot多数据源
7springboot配置druid监听
8springboot自定义注解
9springboot常见注解详解
10springboot接收参数详解
11springboot验证机制@Valid和@Validated
12springboot集成Swagger2
13springboot集成swagger-bootstrap-ui
14springboot集成shiro
15springboot集成shiro(二)
16springboot集成jwt
17springboot集成ActiveMQ
18springboot缓存机制
🍉🍉🍉 欢迎大家来博客了解更多内容:java乐园 🍉🍉🍉
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/13506.html