请求参数
-
@RequestBody
把前端数据请求体转换为后面的类型对象,并且自动注入
请求的需要是
JSON
格式的。并且需要是
POST
请求。 -
@RequestParam
前端数据传输按照指定的格式就可以。
Get
可以直接使用参数拼接,也可以使用Content-Type: multipart/form-data;
Post
可以使用Content-Type: multipart/form-data;
,也可以使用Content-Type: application/x-www-form-urlencoded
传输的内容不能是
JSON
,也不能是直接使用一个对象来接受,例如,这里Book
是一个对象,无法被@RequestParam
直接解析。public String requestParam(@RequestParam Book book) { }
-
@PathVariable
Restful
风格的参数获取。例如:
/get/{id}
获取括号内的参数
测试
-
@RequestBody
测试@Data public class Book { private Integer id; private String name; } @RequestMapping("/param") @RestController() public class ParamController { @PostMapping("/requestBody") public Book requestBodyTest(@RequestBody Book book) { return book; } }
注意请求的
Content-Type: application/json
.使用
Get
请求。不允许的请求
-
@RequestParam
测试@RequestMapping("/param") @RestController() public class ParamController { @RequestMapping("/requestParam5") public String requestParam5(@RequestParam Integer id, @RequestParam String name) { return id + name; } }
-
Get
测试 -
Post
测试 -
@PathVariable
测试@RequestMapping("/param") @RestController() public class ParamController { @RequestMapping("/get/{id}") public String restfulTest(@PathVariable Long id) { return "" + id; } }
-