Performance: @RequestMapping for base paths
This affects the routing and request handling speed in Spring Boot applications, impacting how quickly the server matches incoming requests to controllers.
@RestController @RequestMapping("/api") public class MyController { @RequestMapping("/users") public String getUsers() { return "users"; } @RequestMapping("/products") public String getProducts() { return "products"; } }
@RestController @RequestMapping("/") public class MyController { @RequestMapping("users") public String getUsers() { return "users"; } @RequestMapping("products") public String getProducts() { return "products"; } }
| Pattern | Route Checks | CPU Overhead | Response Time Impact | Verdict |
|---|---|---|---|---|
| Broad base path '/' | High (checks many routes) | High | Slower response | [X] Bad |
| Specific base path '/api' | Low (checks fewer routes) | Low | Faster response | [OK] Good |
@RequestMapping("/api") on a Spring Boot controller class do?@RequestMapping is placed on a controller class, it sets a base URL path prefix for all methods inside./users for a Spring Boot controller using @RequestMapping?path or directly the value in @RequestMapping. Using @RequestMapping(path = "/users") is valid and clear.method attribute for path. @RequestMapping(basePath = "/users") on the class uses a non-existent attribute basePath.getUser() method?@RequestMapping("/api")
public class UserController {
@RequestMapping("/user")
public String getUser() { return "user"; }
}@RequestMapping("/api"), so all method paths start with /api.getUser() has @RequestMapping("/user"), so the full path is /api + /user = /api/user.@RequestMapping("api")
public class ProductController {
@RequestMapping("/list")
public String listProducts() { return "products"; }
}@Controller or @RestController. Without it, the class is not registered as a handler despite having @RequestMapping.@ResponseBody./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"; }
}@RequestMapping("/shop") and method-level @RequestMapping("/items") combines to /shop/items with full syntax validity (parentheses, quotes, leading slashes).@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).Pass the test and this pattern moves unknown