Spring接收web请求参数的⼏种⽅式1 查询参数
请求格式:url?参数1=值1&参数2=值2...
同时适⽤于GET和POST⽅式
spring处理查询参数的⽅法⼜有⼏种写法:
⽅法⼀:
⽅法参数名即为请求参数名
// 查询参数1
@RequestMapping(value = "/test/query1", method = RequestMethod.GET)
public String testQuery1(String username, String password) {
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
⽅法⼆:
从HttpServletRequest中提取参数
// 查询参数2
@RequestMapping(value = "/test/query2", method = RequestMethod.GET)
public String testQuery2(HttpServletRequest request) {
String username = Parameter("username");
String password = Parameter("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
⽅法三:
⽅法参数名和请求参数名可以不⼀样,通过@RequestParam注解来绑定参数
// 查询参数3
@RequestMapping(value = "/test/query3", method = RequestMethod.GET)
public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {
System.out.println("username=" + un + ", password=" + pw);
return "username=" + un + ", password=" + pw;
}
⽅法四:
创建⼀个实体类对象作为参数承载体,spring会根据参数名称⾃动将参数绑定到实体类对象的属性上
// 查询参数4
@RequestMapping(value = "/test/query4", method = RequestMethod.GET)
public String testQuery4(User user) {
String username = Username();
String password = Password();
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
实体类定义如下:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
private String username;
private String password;
}
这⾥⽤到了第三⽅库lombok,这样就不需要在代码中⼿动添加get、set等⽅法,lombok会⾃动添加。
发送请求的curl命令如下:
curl -i '192.168.1.14:8080/test/query1?username=aaa&password=bbb'
交互报⽂如下:
GET /test/query1?username=aaa&password=bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:01:30 GMT
username=aaa, password=bbb
2 表单参数
请求参数不在url中,⽽是在Body体中,格式为:url?参数1=值1&参数2=值2...
适⽤于POST⽅式
spring到底是干啥的表单参数处理⽅法和前⾯的请求参数处理⽅法⼏乎完全⼀样,只是RequestMethod注解中将method⽅法设置成POST⽅法⽅法⼀:
// 表单参数1
@RequestMapping(value = "/test/form1", method = RequestMethod.POST)
public String testForm1(String username, String password) {
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
⽅法⼆:
// 表单参数2
@RequestMapping(value = "/test/form2", method = RequestMethod.POST)
public String testForm2(HttpServletRequest request) {
String username = Parameter("username");
String password = Parameter("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
⽅法三:
// 表单参数3
@RequestMapping(value = "/test/form3", method = RequestMethod.POST)
public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) {
System.out.println("username=" + un + ", password=" + pw);
return "username=" + un + ", password=" + pw;
}
⽅法四:
// 表单参数4
@RequestMapping(value = "/test/form4", method = RequestMethod.POST)
public String testForm4(User user) {
String username = Username();
String password = Password();
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
curl请求命令如下:
curl -X POST -i -d "username=aaa&password=bbb" 192.168.1.14:8080/test/form1
请求和响应报⽂如下:
POST /test/form1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded
username=aaa&password=bbb
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:05:35 GMT
username=aaa, password=bbb
3 路径参数
请求参数为url中的⼀部分,格式为:url/参数1/参数2...
同时适⽤于GET和POST⽅式
代码如下:
@RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)
public String testUrl(@PathVariable String username, @PathVariable String password) {
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
请求curl命令如下:
curl -i 192.168.1.14:8080/test/url/aaa/bbb
请求和响应报⽂如下:
GET /test/url/aaa/bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:07:44 GMT
username=aaa, password=bbb
4 json格式参数
请求参数在Body体中,并且为json格式。需要添加请求头:Content-Type: application/json;charset=UTF-8适⽤于POST⽅式
⽅法⼀:
定义实体类,将json对象解析成实⼒类,需要添加RequestBody注解
// json参数1
@RequestMapping(value = "/test/json1", method = RequestMethod.POST)
public String testJson1(@RequestBody User user) {
String username = Username();
String password = Password();
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
⽅法⼆:
如果不像定义实体类,也可以将json请求直接解析成JSONObject类
// json参数2
@RequestMapping(value = "/test/json2", method = RequestMethod.POST)
public String testJson2(@RequestBody JSONObject json) {
String username = String("username");
String password = String("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
⽅法三:
也可以将json对象直接解析成Map对象
// json参数3
@RequestMapping(value = "/test/json3", method = RequestMethod.POST)
public String testJson3(@RequestBody Map<String, String> userMap) {
String username = ("username");
String password = ("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}
请求curl命令如下:
curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '
{
"username" : "aaa",
"password" : "bbb"
}
' 192.168.1.14:8080/test/json1
请求和响应报⽂如下:
POST /test/json1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json;charset=UTF-8 Content-Length: 52
{
"username" : "aaa",
"password" : "bbb"
}
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8 Content-Length: 26
Date: Thu, 25 Oct 2018 07:09:06 GMT username=aaa, password=bbb

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。