0
0
Spring Bootframework~10 mins

Request mapping by method and path in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Request mapping by method and path
Client sends HTTP request
Spring Boot Dispatcher
Check HTTP method (GET, POST, etc.)
Match request path to @RequestMapping path
Call matching controller method
Return response to client
Spring Boot listens for HTTP requests, matches the method and path to controller methods annotated with @RequestMapping, then runs the matched method.
Execution Sample
Spring Boot
@RestController
@RequestMapping("/api")
public class MyController {
  @GetMapping("/hello")
  public String sayHello() {
    return "Hello";
  }
}
Defines a controller that responds to GET requests at path /api/hello with "Hello".
Execution Table
StepHTTP MethodRequest PathMatch @RequestMappingController Method CalledResponse
1GET/api/helloMatches class @RequestMapping("/api") and method @GetMapping("/hello")sayHello()"Hello"
2POST/api/helloMatches class @RequestMapping("/api") but no POST method for /helloNone404 Not Found
3GET/api/goodbyeMatches class @RequestMapping("/api") but no GET method for /goodbyeNone404 Not Found
💡 Execution stops after matching request method and path or returning 404 if no match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
HTTP MethodNoneGETPOSTGET
Request PathNone/api/hello/api/hello/api/goodbye
Matched Controller MethodNonesayHello()NoneNone
ResponseNone"Hello"404 Not Found404 Not Found
Key Moments - 2 Insights
Why does a POST request to /api/hello return 404 even though the path matches?
Because the controller only has a GET method mapped for /hello, no POST method exists. See execution_table row 2.
How does Spring Boot combine class-level and method-level @RequestMapping paths?
It joins the class-level path (/api) with the method-level path (/hello) to form the full path (/api/hello). See execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is returned for a GET request to /api/hello?
A404 Not Found
B"Hello"
C500 Internal Server Error
DEmpty response
💡 Hint
Check execution_table row 1 under Response column.
At which step does the request method not match any controller method?
AStep 1
BStep 3
CStep 2
DNone
💡 Hint
Look at execution_table rows 2 and 3 for method matching.
If you add a @PostMapping("/hello") method, what changes in the execution table?
AStep 2 would call the new method and return a response instead of 404
BStep 1 would change response
CStep 3 would match the new method
DNo changes
💡 Hint
Adding a POST method for /hello affects POST requests at that path (see Step 2).
Concept Snapshot
Use @RequestMapping or shortcut annotations (@GetMapping, @PostMapping) to map HTTP methods and paths.
Class-level @RequestMapping sets a base path.
Method-level mappings add to the base path.
Spring matches incoming requests by method and path to call the right controller method.
If no match, returns 404 Not Found.
Full Transcript
In Spring Boot, you map HTTP requests to controller methods using @RequestMapping and its shortcuts like @GetMapping. The class-level @RequestMapping sets a base path, and method-level annotations add to it. When a request comes in, Spring checks the HTTP method and path to find the matching method. If found, it runs that method and returns the response. If not, it returns 404. For example, a GET request to /api/hello matches a method annotated with @GetMapping("/hello") inside a class annotated with @RequestMapping("/api"). A POST request to the same path returns 404 if no POST method exists. This flow ensures your app responds correctly to different HTTP methods and paths.