简介
注解在方法上面,表示通过URL路径即可调用其方法。
定义
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
String[] value() default {};
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
语法
-
@RequestMapping(value = “/login”)
-
@RequestMapping(path = “/login”, method=RequestMethod.POST)
-
@RequestMapping(path = “/login”, method={RequestMethod.POST,RequestMethod.GET})
-
@RequestMapping(path = “/login”, params={“username=kolbe”,“password=123456”})
该方法将接收 /login 发来的请求,且请求参数必须为 username=kolbe&password=123456
-
@RequestMapping(path = “/login”, headers=“Host=localhost:8080”)
该方法将接收 /login 发来的请求,且请求头必须带有:Host=localhost:8080,这里限制只接收本地请求
-
@RequestMapping(value = “/login”, method = RequestMethod.POST, consumes=“application/json”)
方法仅处理请求中 Content-Type 请求头为 application/json 类型的请求
-
@RequestMapping(value = “/login”, method = RequestMethod.GET, produces=“application/json”)
方法仅处理请求中 Accept 请求头中包含了 application/json 的请求,同时暗示了返回的内容类型为 application/json
-
路径参数:
@RequestMapping(value="/{id}", method=RequestMethod.GET) public String show(@PathVariable("id") Integer id) { return "success"; }