카테고리 없음

PathVariable

별초롱언니 2025. 6. 17. 17:30

✅ @PathVariable이란?

URL 경로의 일부 값을 변수로 받아오는 어노테이션

 

🔹 예

/books/IT

이 URL에서 "IT"는 카테고리 정보를 나타내는 값

@GetMapping("/books/{category}")
public String getBooksByCategory(@PathVariable String category) {
    System.out.println("카테고리: " + category);
    return "books";
}

📌 여기서 중요한 건:

  • {category}는 URL 패턴 안의 변수
  • @PathVariable String category가 그 값을 받아옴

✅ 여러 개 받을때

 
@GetMapping("/books/{category}/{bookId}") public String getBookDetail(@PathVariable String category, @PathVariable String bookId) { System.out.println("카테고리: " + category); System.out.println("도서 ID: " + bookId); return "bookDetail"; }

 

🔹 예

/books/IT/1001

→ category = "IT", bookId = "1001"