@PathVariable과 @RequestParam의 차이점과 실무 활용법
// URL: /users/123
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id); // id = 123
}
// URL: /posts/5/comments/10
@GetMapping("/posts/{postId}/comments/{commentId}")
public Comment get(@PathVariable Long postId, @PathVariable Long commentId) {
// postId = 5, commentId = 10
}
// URL: /search?keyword=spring&page=2
@GetMapping("/search")
public List<Post> search(
@RequestParam String keyword,
@RequestParam int page
) {
// keyword = "spring", page = 2
}
// 기본값 설정
@RequestParam(defaultValue = "1") int page
@RequestParam(required = false) String keyword