📖 Scenario: You are building a simple web service for a bookstore. The service needs to respond differently based on the HTTP method and the URL path.
🎯 Goal: Create a Spring Boot controller that maps requests by HTTP method and path. You will define a controller class, configure a base path, add methods to handle GET and POST requests at specific paths, and complete the controller with proper annotations.
📋 What You'll Learn
Create a controller class named BookController
Add a base request mapping path /books to the controller
Add a method to handle GET requests at path /list
Add a method to handle POST requests at path /add
Use correct Spring annotations for request mapping by method and path
💡 Why This Matters
🌍 Real World
Web services often need to respond differently based on HTTP method and URL path. This project shows how to set up those mappings in Spring Boot.
💼 Career
Understanding request mapping is essential for backend developers working with Spring Boot to build REST APIs.
Progress0 / 4 steps
1
Create the controller class
Create a public class named BookController annotated with @RestController.
Spring Boot
Hint
Use @RestController above the class to make it a REST controller.
2
Add base request mapping path
Add a class-level @RequestMapping annotation with the path /books to the BookController class.
Spring Boot
Hint
Use @RequestMapping("/books") above the class to set the base path.
3
Add GET method for listing books
Inside BookController, add a public method named listBooks that returns a String. Annotate it with @GetMapping("/list").
Spring Boot
Hint
Use @GetMapping("/list") to map GET requests to /books/list.
4
Add POST method for adding a book
Inside BookController, add a public method named addBook that returns a String. Annotate it with @PostMapping("/add").
Spring Boot
Hint
Use @PostMapping("/add") to map POST requests to /books/add.
Practice
(1/5)
1. What does the @RequestMapping annotation do in Spring Boot?
easy
A. It styles the HTML page returned by the controller.
B. It creates a new database connection automatically.
C. It links a URL path and HTTP method to a specific method in a controller.
D. It schedules background tasks to run periodically.
Solution
Step 1: Understand the purpose of @RequestMapping
@RequestMapping is used to connect web requests to controller methods in Spring Boot.
Step 2: Identify what it links
It links both the URL path and the HTTP method (GET, POST, etc.) to the method that should handle the request.
Final Answer:
It links a URL path and HTTP method to a specific method in a controller. -> Option C
A. The method attribute should use RequestMethod.POST, not a string.
B. The path attribute cannot be used with @RequestMapping.
C. The method must return void, not String.
D. The method name must be saveDataPost, not saveData.
Solution
Step 1: Check method attribute type
The method attribute expects a RequestMethod enum value, not a string.
Step 2: Identify correct usage
Using method = "POST" is invalid; it should be method = RequestMethod.POST.
Final Answer:
The method attribute should use RequestMethod.POST, not a string. -> Option A
Quick Check:
method attribute needs RequestMethod enum [OK]
Hint: Use RequestMethod.POST enum, not string "POST" [OK]
Common Mistakes:
Passing HTTP method as string instead of enum
Thinking path attribute is invalid
Assuming return type or method name causes error
5. You want to create two methods in a controller: one handles GET requests at "/items" and another handles POST requests at the same path "/items". Which is the best way to do this?
hard
A. Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST).
B. Use one method with @RequestMapping(path = "/items") and check HTTP method inside the method manually.
C. Use @RequestMapping(path = "/items") on both methods without specifying method attribute.
D. Use @RequestMapping(method = RequestMethod.GET) on one method and @RequestMapping(method = RequestMethod.POST) on the other without path.
Solution
Step 1: Understand how to map same path with different methods
Spring allows multiple methods mapped to the same path but different HTTP methods using method attribute.
Step 2: Evaluate options
Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST). correctly uses two methods with same path but different HTTP methods. Use one method with @RequestMapping(path = "/items") and check HTTP method inside the method manually. is less clean and error-prone. Use @RequestMapping(path = "/items") on both methods without specifying method attribute. causes ambiguity. Use @RequestMapping(method = RequestMethod.GET) on one method and @RequestMapping(method = RequestMethod.POST) on the other without path. misses path, so won't map correctly.
Final Answer:
Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST). -> Option A
Quick Check:
Separate methods with same path, different HTTP methods [OK]
Hint: Map same path with different methods by specifying method attribute [OK]
Common Mistakes:
Not specifying method attribute causing conflicts
Checking HTTP method manually inside one method
Omitting path attribute causing mapping errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.