使用带有 Post 请求和 Content-Type application/x-www-form-urlencoded 的 HTTP 开发客户端
1)仅@RequestBody
网址:本地主机:8080/SpringMVC/欢迎
正文:名称=abc
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestBody String body, Model model) {
model.addAttribute("message", body);
return "hello";
}
// Gives body as 'name=abc' as expected
2)仅@RequestParam
网址:本地主机:8080/SpringMVC/欢迎
在正文中 - name=abc
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestParam String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
// Gives name as 'abc' as expected
3)两者一起
网址:本地主机:8080/SpringMVC/欢迎
正文:名称=abc
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
@RequestBody String body,
@RequestParam String name, Model model)
{
model.addAttribute("name", name);
model.addAttribute("message", body);
return "hello";
}
// HTTP Error Code 400 - The request sent by the client was syntactically incorrect.
4) 以上参数位置已更改
网址:本地主机:8080/SpringMVC/欢迎
正文:名称=abc
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
@RequestParam String name,
@RequestBody String body, Model model)
{
model.addAttribute("name", name);
model.addAttribute("message", body);
return "hello";
}
// No Error. Name is 'abc'. body is empty
5) 一起但获取类型url参数
URL: localhost:8080/SpringMVC/welcome?name=xyz
正文:名称=abc
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
@RequestBody String body,
@RequestParam String name, Model model)
{
model.addAttribute("name", name);
model.addAttribute("message", body);
return "hello";
}
// name is 'xyz' and body is 'name=abc'
6) 与 5) 相同,但参数位置改变
@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
@RequestParam String name,
@RequestBody String body, Model model)
{
model.addAttribute("name", name);
model.addAttribute("message", body);
return "hello";
}
// name = 'xyz,abc' body is empty
有人可以解释这种行为吗?