Request Mapping 从字面意思可以理解为请求映射的意思。
@RequestMapping 是 Spring Web Mvc 框架用于将Web请求路径映射到 Controller 控制器方法的注解。
通过在控制器的类、方法上添加 @RequestMapping 注解,我们可以指定该方法可以处理的请求路径(URL),以及对应的 HTTP 方法。
@RequestMapping 注解不仅可以用来映射请求路径,还可以配置其他参数,如请求方法、请求参数、请求头等,以实现更精确的请求映射。
一、@RequestMapping注解源码
/** * 用于将 Web 请求映射到 Controller 方法的注解 */@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Mappingpublic @interface RequestMapping {
/** * 为此映射分配一个名称 */ String name() default "";
/** * 此注解表达的主要映射 * path 的别名,@RequestMapping("/foo") 等效于@RequestMapping(path="/foo") */ @AliasFor("path") String[] value() default {};
/** * 路径映射的 URI(例如 "/myPath.do")。 * 还支持 Ant 风格的路径模式(例如 "/myPath/*.do")。 * 在方法级别上,还支持相对路径(例如 "edit.do"),这些路径在类型级别表达的主要映射内。 * 路径映射的 URI 可能包含占位符(例如 "/${connect}") */ @AliasFor("value") String[] path() default {};
/** * 要映射到的 HTTP 请求方法,缩小主要映射范围: * GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE、TRACE */ RequestMethod[] method() default {};
/** * 映射请求的参数,缩小主要映射范围。 */ String[] params() default {};
/** * 映射请求的头部,缩小主要映射范围。 * headers = "content-type=text/*",将匹配具有 Content-Type 为 "text/html"、"text/plain" 等的请求 */ String[] headers() default {};
/** * 映射请求的可消耗媒体类型,缩小主要映射范围。* 示例: * consumes = "text/plain" * consumes = {"text/plain", "application/*"} * 表达式可以通过使用 "!" 操作符来否定,例如 "!text/plain" */ String[] consumes() default {};
/** * 映射请求的可生成媒体类型,缩小主要映射范围。 * 示例: * produces = "text/plain" * produces = {"text/plain", "application/*"} * produces = MediaType.APPLICATION_JSON_UTF8_VALUE* 表达式可以通过使用 "!" 操作符来否定 */ String[] produces() default {};}
@RequestMapping注解既可以标注在类上,又可以标注在方法上。
二、@RequestMapping注解的变体
根据 Http 请求方法 RequestMethod 的不同,@RequestMapping 又产生了5 种专门标注方法的变体。
1、@GetMapping,专门用于 Http GET请求。
@GetMapping源码:
@GetMapping的源码标满了@RequestMapping。
@GetMapping = @RequestMapping(method = RequestMethod.GET),只可标注在方法上。
2、@PostMapping,专门用于 Http POST 请求。
@PostMapping = @RequestMapping(method = RequestMethod.POST),只可标注在方法上。
3、@PutMapping,专门用于 Http PUT 请求。
@PutMapping = @RequestMapping(method = RequestMethod.PUT),只可标注在方法上。
4、@DeleteMapping,专门用于 Http DELETE 请求。
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE),只可标注在方法上。
5、@PatchMapping,专门用于 Http PATCH 请求。
@PatchMapping = @RequestMapping(method = RequestMethod.PATCH),只可标注在方法上。
这5种注解简化了代码,并提高了可读性,使开发者能更清晰地表达其意图。
三、用法整理
1、常用示例
假设我们有一个控制器类,用于处理用户相关的请求:
@RestController@RequestMapping("/users")public class UserController {
@GetMapping public List<User> getAllUsers() { // 返回所有用户的信息 }
@PostMapping public User createUser(@RequestBody User user) { // 创建新用户 }
@GetMapping("/{id}") public User getUserById(@PathVariable Long id) { // 根据ID返回特定用户的信息 }
@PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { // 根据ID更新用户信息 }
@DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { // 根据ID删除用户 }}
在上面的例子中,我们使用@RequestMapping注解在类级别指定了路径"/users",表示该控制器处理所有以"/users"开头的请求。
然后,我们在方法级别使用@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注解来指定具体的HTTP请求类型和路径,以及@PathVariable和@RequestBody注解来处理路径参数和请求体。
这样,我们就可以根据不同的请求类型和路径,来调用相应的方法处理用户请求。
2、consumes、produces属性应用
consumes属性,是针对客户端请求的限制,在这里客户端的请求数据类型只能是application/json,才能被addUser方法接收。
@PostMapping(path = "/Users", consumes = "application/json")public void addUser(@RequestBody User user) { // ...}
produces属性,是响应给客户端数据类型的限制,在这里响应客户端的数据类型为application/json。
@GetMapping(path = "/user/{userUid}", produces = "application/json")@ResponseBodypublic User getUser(@PathVariable String userUid) { // ...}
其实在实际使用中,我们会发现,当我们标注了@RequestBody或@ResponseBody注解时,consumes、produces属性可写可不写。
3、params属性应用
params属性可以用于指定请求必须包含的参数及其条件。
例如,如果要求请求必须包含名为"param"且值为"value"的参数,可以这样使用:
@GetMapping(value = "/example", params = "param=value")public ResponseEntity<String> exampleMethod() { // 处理请求}
这样,只有在请求中包含名为"param"且值为"value"的参数时,才会调用exampleMethod()方法。
4、headers属性
headers属性用于指定请求必须包含的标头及其条件。
例如,如果要求请求必须包含特定的标头,可以这样使用:
@GetMapping(value = "/example", headers = "X-Request-Header=headerValue")public ResponseEntity<String> exampleMethod() { // 处理请求}
这样,只有在请求中包含名为"X-Request-Header"且值为"headerValue"的标头时,才会调用exampleMethod()方法。
5、path/value属性
path属性用于指定请求的路径,与value属性功能相同,可以用来定义更简洁的路径。
例如:
@GetMapping(path = "/example")public ResponseEntity<String> exampleMethod() { // 处理请求}
这样就指定了GET请求的路径为"/example"时,调用exampleMethod()方法。
6、name属性
name属性用于为请求映射命名,可以在客户端调用、文档生成或其他地方引用该名称。
例如:
@GetMapping(value = "/example", name = "ExampleEndpoint")public ResponseEntity<String> exampleMethod() { // 处理请求}
这样就为GET请求的"/example"路径的处理方法命为"ExampleEndpoint",可以在其他地方引用该名称。
注意事项:如果@RequestMapping的path属性位于类上,那么在调用类中的方法时,都需要加上该path。
领取专属 10元无门槛券
私享最新 技术干货