Restful接口传参方式
1.get/delete请求使用RequestParam
-
请求的url类似于http://localhost:8080/springmvc/passValue/rest/param?name=root&password=123,
-
接收方法
@GetMapping("/param") public String getByParam(@RequestParam String name, @RequestParam String password) (PS:@RequestParam是默认接收参数的方式,可以不写) @DeleteMapping("/param") public String deleteByParam(@RequestParam String name, @RequestParam String password)
2.get/delete请求使用PathVariable
-
请求的url类似于http://localhost:8080/springmvc/passValue/rest/root/123,
-
接收方法
@GetMapping("/path/{name}/{password}") public String getByPath(@PathVariable String name, @PathVariable String password) @DeleteMapping("/path/{name}/{password}") public String deleteByPath(@PathVariable String name, @PathVariable String password)
3.post/put请求boby使用RequestBoby
-
请求的url类似于http://localhost:8080/springmvc/passValue/rest/body,
-
接收方法
@PostMapping("/body") public String postByBody(@RequestBody User user) @PutMapping("/body") public String putByBody(@RequestBody User user)
springmvc中@RequestBoby注解接受请求参数
一、POST请求的四种常用方式
1、application/x-www-form-urlencoded
浏览器原生的表单,值为urlencoded之后的 key1=value1&key2=value2…
2、multipart/form-data
浏览器原生的文件表单,用于传输文件
3、application/json
常用的请求头格式,值为json串 {“key1”:“value1”,“key2”:“value2”}
二、@requestbody的接收
1、application/x-www-form-urlencoded
@requestbody能解析,但springmvc会进行解析,所以通常不用@requestbody。
2、multipart/form-data
@requestbody不能解析
3、application/json,application/xml等
@requestbody能解析,springmvc不会进行解析,所以必须要加@requestbody注解
一句话概括:@requestbody能解析json等格式,springmvc只能解析原生表单