4

@PathVariable和@RequestParam的区别

 2 years ago
source link: https://segmentfault.com/a/1190000041151001
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

@PathVariable和@RequestParam的区别

发布于 今天 14:48

简言之,@PathVariable用于接收在url路径中的参数,@RequestParam用于接收请求中的参数。
例如:

@RequestMapping("/hello/{id}")
public String getDetails(@PathVariable(value="id") String id,
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2) {
...
}

@RequestParam 支持下面四种参数
defaultValue 如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值
name 绑定本次参数的名称,要跟URL上面的一样
required 这个参数是不是必须的
value 跟name一样的作用,是name属性的一个别名

而@PathVariable 比@RequestParam少一个defaultValue


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK