Spring常用注解

2018-08-28  本文已影响0人  zjk_00
常用注解.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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读