Discover how a simple annotation saves you from endless URL typing and mistakes!
Why @RequestMapping for base paths in Spring Boot? - Purpose & Use Cases
Imagine writing a web app where every controller method needs the full URL path repeated, like '/api/users', '/api/products', '/api/orders'. You have to type '/api' again and again for each method.
Manually repeating the base path in every method is tiring and error-prone. If you want to change '/api' to '/service', you must update every method. This wastes time and risks missing some places.
@RequestMapping at the class level lets you set a common base path once. Then, each method only adds its specific part. This keeps code clean and easy to update.
@GetMapping("/api/users") public List<User> getUsers() { ... } @GetMapping("/api/products") public List<Product> getProducts() { ... }
@RestController @RequestMapping("/api") public class ApiController { @GetMapping("/users") public List<User> getUsers() { ... } @GetMapping("/products") public List<Product> getProducts() { ... } }
This makes your code easier to maintain and update, especially when many endpoints share the same base path.
In a shopping app, all API calls start with '/api'. Using @RequestMapping at the class level means you can change '/api' to '/shop/api' in one place if needed.
Repeating base paths manually is slow and error-prone.
@RequestMapping at class level sets a shared base path for all methods.
This keeps code cleaner and easier to maintain.
Practice
@RequestMapping("/api") on a Spring Boot controller class do?Solution
Step 1: Understand class-level @RequestMapping
When@RequestMappingis 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 CQuick Check:
Class-level @RequestMapping sets base path = A [OK]
- Confusing base path with HTTP method type
- Thinking it maps all methods to root path
- Assuming it disables mapping
/users for a Spring Boot controller using @RequestMapping?Solution
Step 1: Check correct syntax for base path
The correct attribute to set the path ispathor 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 usesmethodattribute for path. @RequestMapping(basePath = "/users") on the class uses a non-existent attributebasePath.Final Answer:
@RequestMapping(path = "/users") on the class -> Option BQuick Check:
Use path attribute with leading slash [OK]
- Using wrong attribute like method= for path
- Using wrong attribute name like basePath
- Setting base path only on methods
getUser() method?@RequestMapping("/api")
public class UserController {
@RequestMapping("/user")
public String getUser() { return "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 methodgetUser()has@RequestMapping("/user"), so the full path is/api+/user=/api/user.Final Answer:
/api/user -> Option DQuick Check:
Base path + method path = full URL [OK]
- Ignoring class-level path
- Mixing order of paths
- Missing leading slash
@RequestMapping("api")
public class ProductController {
@RequestMapping("/list")
public String listProducts() { return "products"; }
}Solution
Step 1: Identify missing controller annotation
The class is missing@Controlleror@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 AQuick Check:
Controller annotation required [OK]
- Missing @RestController or @Controller annotation
- Thinking method paths are disallowed
- Confusing @Controller with @RestController requirement
/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"; }
}Solution
Step 1: Identify the correct configuration
The setup with class-level@RequestMapping("/shop")and method-level@RequestMapping("/items")combines to/shop/itemswith 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 onshopanditems(syntax error).Final Answer:
Option A correctly sets base path and method path with leading slashes. -> Option AQuick Check:
Base path + method path + valid syntax = full URL [OK]
- Missing parentheses in @RequestMapping
- Missing quotes around path values
- Omitting leading slashes on paths
Ready to prove it?
Pass the test and this pattern moves unknown
