@RequestMapping 的作用是什么?

2019-09-22  

@RequestMapping 是一个注解,用来标识 http 请求地址与 Controller 类的方法之间的映射。

 

可作用于类和方法上,方法匹配的完整是路径是 Controller 类上 @RequestMapping 注解的 value 值加上方法上的 @RequestMapping 注解的 value 值。 

/**
 * 用于映射url到控制器类或一个特定的处理程序方法.
 */
//该注解只能用于方法或类型上
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
 
    /**
     * 指定映射的名称
     */
    String name() default "";
 
    /**
     * 指定请求的路径映射,别名为path
     */
    @AliasFor("path")
    String[] value() default {};
 
    /** 
	 * 别名为 value,使用 path 更加形象
     */
    @AliasFor("value")
    String[] path() default {};
 
    /**
     * 指定 http 请求的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. 
     */
    RequestMethod[] method() default {};
 
    /**
     * 指定映射 http 请求的参数
     */
    String[]params() default {};
 
    /**
     * 指定处理的 http 请求头
     */
    String[] headers() default {};
 
    /**
     * 指定处理的请求提交内容类型(Content-Type)
     */
    String[] consumes() default {};
 
    /**
     * 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
     */
    String[] produces() default {};
}

 

指定 http 请求的类型使用

public enum RequestMethod {
 
	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
 
}

 

 

ConstXiong 备案号:苏ICP备16009629号-3