相关疑难解决方法(0)

Spring MVC - 为什么不能一起使用@RequestBody和@RequestParam

使用带有Post请求的HTTP dev客户端和Content-Type application/x-www-form-urlencoded

1)只有@RequestBody

请求 - localhost:8080/SpringMVC/welcome In Body - name = abc

码-

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestBody String body, Model model) {
    model.addAttribute("message", body);
    return "hello";
}
Run Code Online (Sandbox Code Playgroud)

//按预期将body标记为"name = abc"

2)只有@RequestParam

请求 - localhost:8080/SpringMVC/welcome In Body - name = abc

码-

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestParam String name, Model model) {
    model.addAttribute("name", name);
    return "hello";
}
Run Code Online (Sandbox Code Playgroud)

//按预期将名称命名为"abc"

3)两者在一起

请求 - localhost:8080/SpringMVC/welcome In Body - name = abc

码-

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestBody String body, …
Run Code Online (Sandbox Code Playgroud)

post spring spring-mvc http-post http-request-parameters

56
推荐指数
2
解决办法
10万
查看次数