使用rest编程风格时,可以直接将变量值放入到url中,传递到后台,后台自动识别对应的方法,方便很多。
但若出现方法重载的情况,则可能会出问题,如下
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService us;
@ResponseBody
@GetMapping("/{username}")
public User getUser(@PathVariable("username")String username) {
return us.getUser(username);
}
@ResponseBody
@GetMapping("/{id}")
public User getUser(@PathVariable("id")Integer id) {
return us.findUserById(id);
}
}
在浏览器地址栏访问 http://localhost:8080/user/1 或 http://localhost:8080/user/lucy 时均会出现如下错误