@RequestMapping源碼
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {String name() default "";@AliasFor("path")String[] value() default {};@AliasFor("value")String[] path() default {};RequestMethod[] method() default {};String[] params() default {};String[] headers() default {};String[] consumes() default {};String[] produces() default {};}
?通過 @RequestMapping 源碼,我們得知可以配置以下屬性:
- name
- value(path)
- method
- params
- headers
- consumes
- produces
name
映射的名稱
名稱可以指定也可以不指定,規則如下:
- 指定:配置的映射名
- 未指定:類名中的大寫字母 + # + 方法名
效果演示
創建Controller
@RestController
@RequestMapping("/mapping")
public class MappingController {@GetMapping(value = "/default_name")public String defaultName() {return "default_name";}@GetMapping(value = "/custom_name", name = "MappingController#customName")public String customName() {return "custom_name";}}
AbstractHandlerMethodMapping#lookupHandlerMethod
value(path)
請求路徑
如果類和方法上都存在?@RequestMapping 注解,請求路徑 = 類上配置的path + 方法上配置的path
配置多個請求路徑
我們可以讓多個請求路徑,映射到同一個接口方法
案例演示
@GetMapping(value = {"/p1", "/p2"})
public String multiPath() {return "multiPath";
}
添加前綴
我們也可以添加配置,使得特定 Controller 的請求路徑需要添加前綴,這時候 請求路徑 = 前綴 +?類上配置的path + 方法上配置的path
案例演示
創建配置類
@Configuration
public class MappingConfig implements WebMvcConfigurer {@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {configurer.addPathPrefix("/custom", type -> type.isAssignableFrom(MappingController.class));}
}
請求 /mapping/p1 拋出 404 異常
請求 /custom/mapping/p2?正常映射
支持Spel表達式
創建 keys.properties
key=a
引用keys.properties
@SpringBootApplication
@PropertySource("classpath:keys.properties")
public class BootApplication {public static void main(String[] args) {SpringApplication.run(BootApplication.class);}
}
?接口及響應
@GetMapping(value = {"${key}"})
public String spel() {return "spel";
}
method
請求方法
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping 等注解都是?@RequestMapping 注解的封裝
- @GetMapping ==> @RequestMapping(method = RequestMethod.GET)
- @PostMapping ==> @RequestMapping(method = RequestMethod.POST)
- @PutMapping ==>?@RequestMapping(method = RequestMethod.PUT)
- @DeleteMapping ==>?@RequestMapping(method = RequestMethod.DELETE)
params
The parameters of the mapped request, narrowing the primary mapping.
Same format for any environment: a sequence of "myParam=myValue" style expressions, with a request only mapped if each such parameter is found to have the given value. Expressions can be negated by using the "!=" operator, as in "myParam!=myValue". "myParam" style expressions are also supported, with such parameters having to be present in the request (allowed to have any value). Finally, "!myParam" style expressions indicate that the specified parameter is not supposed to be present in the request.
通過注釋,我們得知?Expressions 大概有以下四種形式:
- myParam=myValue : 某個參數的值等于指定value
- myParam!=myValue: 某個參數的值不等于指定value
- myParam : 存在參數myParam
- !myParam : 不存在參數myParam
案例演示
myParam=myValue
接口及響應
@GetMapping(value = "equal_value", params = {"key=1"})
public String equalValue() {return "equal value";
}
key 不存在
key 為 2
key 為 1?
key 不存在或者 value 不等于指定值則拋出異常
myParam!=myValue
接口及響應
@GetMapping(value = "not_equal_value", params = {"key!=1"})
public String notEqualValue() {return "not equal value";
}
key 不存在
key 為 2
key 為 1
key 不存在或者 value 不等于指定值正常映射,否則拋出異常
myParam
接口及響應
@GetMapping(value = "exist", params = {"key"})
public String exist() {return "exist";
}
key 為 null?
key 為 1?
key 不存在?
只要指定 key 存在就可以正常映射,不管是不是為 null
!myParam
接口及響應
@GetMapping(value = "absent", params = {"!key"})
public String absent() {return "absent";
}
?key 為 null
key 為 1
key 不存在?
只要指定 key 存在就拋出異常,不管是不是為 null
headers
The headers of the mapped request, narrowing the primary mapping.
Same format for any environment: a sequence of "My-Header=myValue" style expressions, with a request only mapped if each such header is found to have the given value. Expressions can be negated by using the "!=" operator, as in "My-Header!=myValue". "My-Header" style expressions are also supported, with such headers having to be present in the request (allowed to have any value). Finally, "!My-Header" style expressions indicate that the specified header is not supposed to be present in the request.
和 params 類似,headers也有四種形式
- My-Header=myValue
- My-Header!=myValue
- My-Header
- !My-Header
和 params 用法基本一致,這里就演示一個案例,不過多舉例
案例演示
My-Header=myValue
接口及響應
@GetMapping(value = "headers", headers = {"Custom-Header=66"})
public String headers() {return "headers";
}
consumes
Content-Type 的值必須滿足 consumes(如果不為空數組) 指定的多個值(或取反)之一
案例演示
接口及響應
@GetMapping(value = "consumes", consumes = {"text/plain", "application/json"})
public String consumes() {return "consumes";
}
PS : 如果?headers 已經對 Content-Type? 進行了限制,consumes 失效
produces
Accept 的值必須滿足(兼容) produces(如果不為空數組) 指定的多個值(或取反)之一
案例演示
接口及響應
@GetMapping(value = "produces", produces = {"text/plain", "application/json"})
public String produces() {return "produces";
}
PS : 如果?headers 已經對 Accept? 進行了限制,produces 失效