@RequestParam,@RequestBody以及@PathVariable的区别

it2025-10-21  9

1.@RequestParam

一般用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。@RequestParam接收的参数是来自requestHeader中,即请求头,例如在url中形如: xxx?username=123456&password=123456 。一般用于GET请求。

2.@RequestBody

一般用来处理application/json、application/xml等类型的数据。注解@RequestBody接收的参数是来自requestBody中,即请求体。通过@RequestBody可以解析Body中json格式的数据。一般用于POST请求。

3.@PathVariable

当接口规范为resultful风格时,可以使用@PathVariable来动态获取参数。例如:

最新回复(0)