@RequestMapping for base paths helps organize your web app by grouping related URLs under one main path. It makes your code cleaner and easier to manage.
You want all URLs in a controller to start with the same base path, like '/users' for user-related actions.
You want to avoid repeating the same path prefix on every method in a controller.
You want to clearly separate different parts of your app by URL structure.
You want to handle all requests under a common path in one place.
Syntax
Spring Boot
@RestController
@RequestMapping("/basepath")
public class YourController {
@RequestMapping("/subpath")
public String method() {
// handle request
return"response";
}
}
The base path is set on the class level.
Method-level @RequestMapping paths are appended to the base path.
Examples
All URLs start with '/api'. The method handles '/api/status'.
Spring Boot
@RestController
@RequestMapping("/api")
public class ApiController {
@RequestMapping("/status")
public String status() {
return"OK";
}
}
Both methods share the base path '/products'. URLs are '/products/list' and '/products/detail'.
Spring Boot
@RestController
@RequestMapping("/products")
public class ProductController {
@RequestMapping("/list")
public String list() {
return"Product list";
}
@RequestMapping("/detail")
public String detail() {
return"Product detail";
}
}
Sample Program
This controller handles requests starting with '/greetings'. The two methods respond to '/greetings/hello' and '/greetings/bye'.
Spring Boot
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/greetings")
public class GreetingController {
@RequestMapping("/hello")
public String sayHello() {
return"Hello!";
}
@RequestMapping("/bye")
public String sayBye() {
return"Goodbye!";
}
}
OutputSuccess
Important Notes
Use @RestController to combine @Controller and @ResponseBody for simple REST APIs.
Base path helps avoid repeating common URL parts on every method.
Paths are case sensitive by default.
Summary
@RequestMapping at class level sets a base path for all methods inside.
Method-level @RequestMapping paths add to the base path.
This keeps URL structure organized and code cleaner.
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
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.
Step 2: Effect on method-level mappings
Each method's path is appended to this base path, organizing URLs under a common root.
Final Answer:
Sets a base URL path for all request methods inside the class. -> Option C
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
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.
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.
Final Answer:
@RequestMapping(path = "/users") on the class -> Option B
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
Step 1: Identify class-level base path
The class has @RequestMapping("/api"), so all method paths start with /api.
Step 2: Append method-level path
The method getUser() has @RequestMapping("/user"), so the full path is /api + /user = /api/user.
Final Answer:
/api/user -> Option D
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
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.
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.
Final Answer:
Class must be annotated with @RestController or @Controller. -> Option A
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
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).
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).
Final Answer:
Option A correctly sets base path and method path with leading slashes. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.