美文网首页
Spring常用注解

Spring常用注解

作者: zjk_00 | 来源:发表于2018-08-28 17:43 被阅读0次
常用注解.png 装配注解比较.png

1.注解@PathVariable和@RequestParam的区别

@RequestParam注解是获取静态URL传入的参数
@PathVariable是获取请求路径中的变量作为参数
如下例子:

@RestController
@RequestMapping(value="users")
public class UserController {
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> list(@RequestParam Long user_id) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_id", user_id);
        return map;
    }
    @RequestMapping(value = "/get_user/{user_id}", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> get_user(@PathVariable Long user_id) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_id", user_id);
        return map;
    }
}

相关文章

网友评论

      本文标题:Spring常用注解

      本文链接:https://www.haomeiwen.com/subject/gqpqwftx.html