Handling path variables and query params together in Spring Boot
📖 Scenario: You are building a simple Spring Boot web service for a bookstore. You want to create an endpoint that takes a book category as a path variable and an optional filter as a query parameter.This will help users get books from a specific category and optionally filter them by author name.
🎯 Goal: Build a Spring Boot controller with a method that handles a path variable category and an optional query parameter author. The method should return a string message showing the received category and author filter.
📋 What You'll Learn
Create a Spring Boot controller class named
BookControllerAdd a GET mapping method named
getBooksByCategoryUse
@PathVariable to get the category from the URL pathUse
@RequestParam to get the optional author query parameter with default value "all"Return a string message showing the category and author filter values
💡 Why This Matters
🌍 Real World
Web services often need to get data from both the URL path and query parameters to filter or customize responses.
💼 Career
Understanding how to handle path variables and query parameters is essential for backend developers building REST APIs with Spring Boot.
Progress0 / 4 steps