SpringMVC 中 @RequestMapping 注解的使用错误
A. @RequestMapping 来指定请求的 url,只能写在方法上(错误)
@RequestMapping 注解可以用来指定请求的 URL,它可以写在类级别和方法级别。
- 类级别: 用于指定该类所有方法的通用请求路径前缀。
- 方法级别: 用于指定具体方法的请求路径。
例如:
@Controller
@RequestMapping("/users") // 类级别
public class UserController {
@GetMapping("/{id}") // 方法级别
public User getUser(@PathVariable Integer id) {
// ...
}
}
在这个例子中,@RequestMapping("/users") 指定了 UserController 类所有方法的通用路径前缀为 /users。@GetMapping("/{id}") 则指定了 getUser 方法的请求路径为 /users/{id}。
所以,选项 A 是错误的。
原文地址: http://www.cveoy.top/t/topic/mJFr 著作权归作者所有。请勿转载和采集!