@PathVariable注解获取url中的参数:
@RequestMapping(value = {"/hello/{id}", "hi"}, method = RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id) {
// return content;
return "id:"+id;
}

@RequestParam注解获取url中的参数:
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
public String sayHello1(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
// return content;
return "id:"+id;
}

本文介绍了在Spring框架中如何使用@PathVariable和@RequestParam注解来从URL中获取参数。通过实例展示了两种注解的不同用法,@PathVariable用于路径变量,而@RequestParam用于查询字符串。
2622

被折叠的 条评论
为什么被折叠?



