Bird
Raised Fist0
Spring Bootframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does placing @RequestMapping("/api") on a Spring Boot controller class do?
easy
A. Automatically maps all methods to the root URL /.
B. Defines the HTTP method type for all requests in the class.
C. Sets a base URL path for all request methods inside the class.
D. Disables URL mapping for the controller.

Solution

  1. Step 1: Understand class-level @RequestMapping

    When @RequestMapping is placed on a controller class, it sets a base URL path prefix for all methods inside.
  2. Step 2: Effect on method-level mappings

    Each method's path is appended to this base path, organizing URLs under a common root.
  3. Final Answer:

    Sets a base URL path for all request methods inside the class. -> Option C
  4. Quick Check:

    Class-level @RequestMapping sets base path = A [OK]
Hint: Class-level @RequestMapping sets base path prefix [OK]
Common Mistakes:
  • Confusing base path with HTTP method type
  • Thinking it maps all methods to root path
  • Assuming it disables mapping
2. Which of the following is the correct way to set a base path /users for a Spring Boot controller using @RequestMapping?
easy
A. @RequestMapping("/users") on each method only
B. @RequestMapping(path = "/users") on the class
C. @RequestMapping(method = "users") on the class
D. @RequestMapping(basePath = "/users") on the class

Solution

  1. Step 1: Check correct syntax for base path

    The correct attribute to set the path is path or directly the value in @RequestMapping. Using @RequestMapping(path = "/users") is valid and clear.
  2. Step 2: Validate other options

    @RequestMapping("/users") on each method only sets path only on methods, not base path. @RequestMapping(method = "users") on the class incorrectly uses method attribute for path. @RequestMapping(basePath = "/users") on the class uses a non-existent attribute basePath.
  3. Final Answer:

    @RequestMapping(path = "/users") on the class -> Option B
  4. Quick Check:

    Use path attribute with leading slash [OK]
Hint: Use path="/yourBase" with leading slash on class [OK]
Common Mistakes:
  • Using wrong attribute like method= for path
  • Using wrong attribute name like basePath
  • Setting base path only on methods
3. Given the controller class below, what is the full URL path to access the getUser() method?
@RequestMapping("/api")
public class UserController {
  @RequestMapping("/user")
  public String getUser() { return "user"; }
}
medium
A. /user
B. /user/api
C. /api
D. /api/user

Solution

  1. Step 1: Identify class-level base path

    The class has @RequestMapping("/api"), so all method paths start with /api.
  2. Step 2: Append method-level path

    The method getUser() has @RequestMapping("/user"), so the full path is /api + /user = /api/user.
  3. Final Answer:

    /api/user -> Option D
  4. Quick Check:

    Base path + method path = full URL [OK]
Hint: Combine class and method paths with slashes [OK]
Common Mistakes:
  • Ignoring class-level path
  • Mixing order of paths
  • Missing leading slash
4. What is wrong with this controller code?
@RequestMapping("api")
public class ProductController {
  @RequestMapping("/list")
  public String listProducts() { return "products"; }
}
medium
A. Class must be annotated with @RestController or @Controller.
B. Missing leading slash in class-level @RequestMapping path.
C. Method-level @RequestMapping cannot have a path.
D. Method must return ResponseEntity, not String.

Solution

  1. Step 1: Identify missing controller annotation

    The class is missing @Controller or @RestController. Without it, the class is not registered as a handler despite having @RequestMapping.
  2. Step 2: Validate other options

    Method-level paths are allowed. Leading slash is optional (Spring prepends it). Returning String is valid for view names or with @ResponseBody.
  3. Final Answer:

    Class must be annotated with @RestController or @Controller. -> Option A
  4. Quick Check:

    Controller annotation required [OK]
Hint: Use @RestController or @Controller on controller classes [OK]
Common Mistakes:
  • Missing @RestController or @Controller annotation
  • Thinking method paths are disallowed
  • Confusing @Controller with @RestController requirement
5. You want all endpoints in your controller to start with /shop, and inside you have a method mapped to /items. Which of these setups correctly maps the full URL /shop/items and why?
Option A:
@RequestMapping("/shop")
public class ShopController {
  @RequestMapping("/items")
  public String getItems() { return "items"; }
}

Option B:
@RequestMapping("/shop/items")
public class ShopController {
  @RequestMapping
  public String getItems() { return "items"; }
}

Option C:
@RequestMapping
public class ShopController {
  @RequestMapping("/shop/items")
  public String getItems() { return "items"; }
}

Option D:
@RequestMapping(shop)
public class ShopController {
  @RequestMapping(items)
  public String getItems() { return "items"; }
}
hard
A. Option A correctly sets base path and method path with leading slashes.
B. Option B sets full path on class, method has no path, works fine.
C. Option C sets full path on method only, class has no base path.
D. Option D misses leading slashes, so paths are invalid.

Solution

  1. Step 1: Identify the correct configuration

    The setup with class-level @RequestMapping("/shop") and method-level @RequestMapping("/items") combines to /shop/items with full syntax validity (parentheses, quotes, leading slashes).
  2. Step 2: Examine incorrect configurations

    The full class path setup lacks parentheses on method @RequestMapping (syntax error). The full method path setup lacks parentheses on class @RequestMapping (syntax error). The no-slash setup lacks quotes on shop and items (syntax error).
  3. Final Answer:

    Option A correctly sets base path and method path with leading slashes. -> Option A
  4. Quick Check:

    Base path + method path + valid syntax = full URL [OK]
Hint: Use leading slashes on class and method paths for clarity [OK]
Common Mistakes:
  • Missing parentheses in @RequestMapping
  • Missing quotes around path values
  • Omitting leading slashes on paths
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard