import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/users") public class UserController { @RequestMapping("/list") public String listUsers() { return "User list"; } @RequestMapping("/detail") public String userDetails() { return "User details"; } }
The @RequestMapping annotation on the class sets the base path for all methods inside the controller. Here, it is /api/v1/users. The methods add to this base path.
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/users") public class UserController { @RequestMapping("/list") public String listUsers() { return "User list"; } @RequestMapping("/detail") public String userDetails() { return "User details"; } }
The full URL path is the base path from the class /api/v1/users plus the method path /list, resulting in /api/v1/users/list.
To specify multiple paths, use an array of strings inside curly braces: {"/api/v1/users", "/users"}. Option A is correct.
If a method has @RequestMapping without a path, it matches the base path set by the class-level @RequestMapping. So accessing "/api/v1/users" returns "User list".
The class-level @RequestMapping sets GET as default method for all paths. The createItem method overrides this with POST. The listItems method inherits GET from the class.
