Discover how a simple annotation can save you from tangled, hard-to-maintain request handling code!
Why Request mapping by method and path in Spring Boot? - Purpose & Use Cases
Imagine building a web app where you have to check every incoming request's URL and HTTP method manually to decide what code to run.
You write long if-else chains to handle GET, POST, PUT, DELETE requests for different paths.
This manual checking is slow to write and easy to mess up.
It becomes hard to read, maintain, and add new routes without breaking existing ones.
Also, mixing URL and method logic in one place makes the code confusing.
Spring Boot's request mapping lets you declare which method handles which HTTP method and path clearly with annotations.
This separates routing logic from business code and makes your app easier to understand and extend.
if (request.getMethod().equals("GET") && request.getPath().equals("/users")) { handleGetUsers(); } else if (request.getMethod().equals("POST") && request.getPath().equals("/users")) { handleCreateUser(); }
@GetMapping("/users") public List<User> getUsers() { ... } @PostMapping("/users") public User createUser() { ... }
You can build clean, scalable web apps where each URL and method pair maps directly to a focused handler method.
Think of an online store where GET /products shows items, POST /products adds a new item, and DELETE /products/{id} removes one.
Request mapping by method and path makes this simple and clear.
Manual request handling is complex and error-prone.
Request mapping annotations cleanly connect URLs and HTTP methods to code.
This improves readability, maintainability, and scalability of web apps.
Practice
@RequestMapping annotation do in Spring Boot?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 CQuick Check:
@RequestMapping links URL and method [OK]
- Thinking @RequestMapping styles HTML output
- Confusing it with database or scheduling annotations
- Assuming it only maps URL without HTTP method
@RequestMapping?Solution
Step 1: Recall correct @RequestMapping syntax
The correct syntax uses 'path' or 'value' for URL and 'method' with RequestMethod enum for HTTP method.Step 2: Match options with syntax
@RequestMapping(path = "/home", method = RequestMethod.GET) uses path and method=RequestMethod.GET correctly. Others use invalid attribute names or wrong types.Final Answer:
@RequestMapping(path = "/home", method = RequestMethod.GET) -> Option BQuick Check:
Correct syntax [OK]
- Using 'url' instead of 'path' or 'value'
- Passing HTTP method as string instead of RequestMethod enum
- Using wrong attribute names like 'route' or 'type'
@RequestMapping(path = "/test", method = RequestMethod.POST)
public String test() {
return "Success";
}
What happens when a client sends a GET request to "/test"?Solution
Step 1: Identify the mapped HTTP method
The method is mapped only to POST requests at path "/test".Step 2: Understand what happens on GET request
A GET request to the same path is not handled, so Spring returns 405 Method Not Allowed.Final Answer:
Spring returns a 405 Method Not Allowed error. -> Option DQuick Check:
GET to POST-only path = 405 error [OK]
- Assuming GET runs the POST method
- Thinking it returns 404 Not Found instead
- Believing server crashes on wrong method
@RequestMapping(path = "/data", method = "POST")
public String saveData() {
return "Saved";
}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 AQuick Check:
method attribute needs RequestMethod enum [OK]
- Passing HTTP method as string instead of enum
- Thinking path attribute is invalid
- Assuming return type or method name causes error
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 AQuick Check:
Separate methods with same path, different HTTP methods [OK]
- Not specifying method attribute causing conflicts
- Checking HTTP method manually inside one method
- Omitting path attribute causing mapping errors
Ready to prove it?
Pass the test and this pattern moves unknown
