@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 失效