0
0
Spring Bootframework~10 mins

@RequestMapping for base paths in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @RequestMapping for base paths
Start Application
Scan Controller Class
Find @RequestMapping at Class Level
Set Base Path for All Methods
Scan Methods with @RequestMapping
Combine Base Path + Method Path
Handle Incoming HTTP Request
Match Request URL to Combined Path
Yes No
Call Method
Send Response
End
The flow shows how Spring Boot reads a controller's base path from @RequestMapping on the class, then combines it with method-level paths to match incoming requests.
Execution Sample
Spring Boot
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class MyController {
  @GetMapping("/hello")
  public String sayHello() {
    return "Hello";
  }
}
This code sets a base path '/api' for all methods in MyController, so the full path for sayHello is '/api/hello'.
Execution Table
StepActionDetailsResult
1Start ApplicationSpring Boot starts and scans componentsApplication context initialized
2Scan Controller ClassFind MyController classClass found
3Find @RequestMapping at Class LevelReads '/api' base pathBase path set to '/api'
4Scan Methods with @RequestMappingFind sayHello method with @GetMapping('/hello')Method path '/hello' found
5Combine Base Path + Method PathCombine '/api' + '/hello'Full path '/api/hello'
6Handle Incoming HTTP RequestRequest to GET '/api/hello'Request received
7Match Request URL to Combined PathCheck if '/api/hello' matchesMatch found
8Call MethodInvoke sayHello()Returns 'Hello'
9Send ResponseSend 'Hello' back to clientResponse sent
10Request to GET '/hello'Request receivedNo match, 404 returned
💡 Execution stops when request matches a combined path or returns 404 if no match.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
basePathnull"/api""/api""/api""/api"
methodPathnullnull"/hello""/hello""/hello"
fullPathnullnull"/api/hello""/api/hello""/api/hello"
requestURLnullnullnull"/api/hello" or "/hello""/api/hello" or "/hello"
matchFoundfalsefalsefalsetrue or falsetrue or false
Key Moments - 3 Insights
Why does the method path '/hello' become '/api/hello'?
Because the class-level @RequestMapping sets a base path '/api' that prefixes all method paths, as shown in execution_table step 5.
What happens if a request comes to '/hello' without the base path?
The request does not match any combined path, so Spring returns a 404 error, as shown in execution_table step 10.
Can multiple methods share the same base path?
Yes, all methods in the controller share the base path from the class-level @RequestMapping, making it easier to organize URLs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the full path after combining base and method paths at step 5?
A"/api"
B"/hello"
C"/api/hello"
D"/hello/api"
💡 Hint
Check the 'Result' column at step 5 in execution_table.
At which step does Spring decide the request matches the controller method?
AStep 4
BStep 7
CStep 9
DStep 10
💡 Hint
Look for 'Match found' in the 'Result' column of execution_table.
If the base path was changed to '/service', what would be the full path for sayHello method?
A"/service/hello"
B"/hello/service"
C"/api/hello"
D"/hello"
💡 Hint
Refer to variable_tracker 'basePath' and how it combines with 'methodPath'.
Concept Snapshot
@RequestMapping at class level sets a base URL path.
All method-level mappings append to this base path.
Incoming requests match combined paths.
If no match, Spring returns 404.
Use base paths to organize related endpoints.
Full Transcript
When a Spring Boot application starts, it scans controller classes for @RequestMapping annotations. If a class has @RequestMapping with a path like '/api', this path becomes the base for all methods inside. Each method can have its own @RequestMapping or shortcut like @GetMapping with a sub-path such as '/hello'. Spring combines the base path and method path to form the full URL, for example '/api/hello'. When a request comes in, Spring checks if the URL matches any combined path. If it matches, the corresponding method is called and its response is sent back. If no match is found, Spring returns a 404 error. This helps organize URLs by grouping related endpoints under a common base path.