@RequestMapping的参数和用法

11 篇文章 1 订阅

在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.xml中配置

源码:

// 该注解说明可以在类和方法上使用
// 用于类上:表示类中的所有响应请求的方法都是以该地址作为父路径
// 用于方法上: 提供进一步的细分映射信息
@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 {};
}

注解中的属性除了 name() 返回的字符串,其它的方法均返回数组,也就是可以定义多个属性值,例如 value() 和 path() 都可以同时定义多个字符串值来接收多个URL请求

属性

value

指定请求的实际地址,指定的地址可以是URI Template 模式

@RequestMapping 中的 value 和 path 属性(这两个属性作用相同,可以互换,如果仅有这一个属性,则可以省略)

普通的具体值

    @RequestMapping("/test1")
    public String test1(){
        return "test";
    }

    @RequestMapping(value = "/test2")
    public String test2(){
        return "test";
    }

    // 多个请求映射到一个方法上
    @RequestMapping(value = {"/test3","/","/test4"})
    public String test3(){
        return "test";
    }

含有某变量的一类值(URI Template Patterns with Path Variables)

    // 请求路径:工程名/test1/path
    @RequestMapping("/test1/{path}")
    public String test1(@PathVariable String path){
        return "test";
    }

含正则表达式的一类值( URI Template Patterns with Regular Expressions)

@RequestMapping(value = "/list/{path:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")  
public BaseDTO list(@PathVariable String path) {
    // TODO
}

method

method 主要用来定义接收浏览器发来的何种请求。在Spring中,使用枚举类

org.springframework.web.bind.annotation.RequestMethod来定义浏览器请求的方式。

HTTP规范定义了多种请求资源的方式,最基本的有四种,分别为:GET(查)、POST(增)、PUT(改)、DELETE(删),而URL则用于定位网络上的资源相当于地址的作用,配合四种请求方式,可以实现对URL对应的资源的增删改查操作。

在实际应用中,很多人并没有按照这个规范做,因为使用GET/POST同样可以完成PUT和DELETE操作,甚至GET也可以完成POST操作,因为GET不需要用到表单,而POST却需要通过表单来发送。


    // 处理GET请求
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2(){
        return "test";
    }

    // 处理POST请求
    @RequestMapping(value = "/test2", method = RequestMethod.POST)
    public String test3(){
        return "test";
    }

由于在 RequestMapping 注解类中 method() 方法返回的是 RequestMethod 数组,所以可以给 method 同时指定多个请求方式,

   @RequestMapping(value = "/test2", method = {RequestMethod.GET, RequestMethod.POST})
    public String test3(){
        return "test";
    }

consumes

指定处理请求的 提交内容类型 (Content-Type),例如 application/json, text/html

    // 方法仅处理请求方式是GET和request Content-Type为“application/json”类型的请求
    @RequestMapping(value = "/test2", method = RequestMethod.GET, consumes = "application/json")
    public String test2(){
        return "test";
    }

produces

指定 返回的内容类型 ,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

@RequestMapping(value = "/list" , method = RequestMethod.POST,produces="application/json")
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

//@responseBody就是返回值是json数据,使用@responseBody,就可以省略produces属性
@RequestMapping(value = "/list" , method = RequestMethod.POST)
@ResponseBody
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

//返回值是json数据,字符编码为utf-8
@RequestMapping(value = "/list" , method = RequestMethod.POST,produces="application/json;charset=utf-8")
public JSONObject list(@PathVariable String communityId) {
   JSONObject object = new JSONObject();
   object.put("communityId",communityId);
   return object;
}

params

该属性表示请求参数,也就是追加在URL上的键值对,多个请求参数以&隔开,指定request中必须包含某些参数值时,才让该方法处理。

    // 设定必须包含username参数,且username=chen
    @RequestMapping(value = "/test", params = "username=chen")
    public String test1(){
        return "test1  chen";
    }

    // 设定必须包含username参数,且username=yang
    @RequestMapping(value = "/test", params = "username=yang")
    public String test2(){
        return "test1  yang";
    }

    // // 设定必须包含username参数,且username=zhao
    @RequestMapping(value = "/test", params = "username=zhao")
    public String test3(){
        return "test1  zhao";
    }

    //设定必须包含username 和age两个参数,且age参数不为10 (可以有多个参数)
    @RequestMapping(value = "/list" ,params = { "username","age!=10" })
    public String list() {
       return "test";
    }

headers

指定request中必须包含某些指定的header值,才能让该方法处理请求。

用于HTTP协义交互的信息被称为HTTP报文,客户端发送的HTTP报文被称为请求报文,服务器发回给客户端的HTTP报文称为响应报文,报文由报文头部和报文体组成。

请求头部(Request Headers):请求头包含许多有关客户端环境和请求正文的信息,例如浏览器支持的语言、请求的服务器地址、客户端的操作系统等。

响应头部(Rsponse Headers):响应头也包含许多有用的信息,包括服务器类型、日期、响应内容的类型及编码,响应内容的长度等等。

//仅处理request的header中包含了指定“Refer”请求头和对应值为“http://www.baidu.com/”的请求;
@RequestMapping(value = "/list" , headers="Referer=http://www.baidu.com/")
public String list() {
   return "test";
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RequestMapping是Spring MVC中用于映射请求路径和处理方法的注解,它可以用于类级别和方法级别。 在方法级别上,@RequestMapping注解可以接收以下一些常用的参数: 1. value/path:指定请求的URL路径。可以是一个字符串或一个字符串数组,用于匹配请求的URL。例如:`@RequestMapping(value = "/users")`。 2. method:指定请求的HTTP方法。可以是RequestMethod枚举类型,或者是RequestMethod枚举类型数组,用于限定请求的HTTP方法。例如:`@RequestMapping(value = "/users", method = RequestMethod.GET)`。 3. params:指定请求的参数条件。可以是一个字符串,用于匹配请求中的参数;或者是一个字符串数组,用于同时匹配多个参数条件。例如:`@RequestMapping(value = "/users", params = "id=1")`。 4. headers:指定请求的头部条件。可以是一个字符串,用于匹配请求中的头部信息;或者是一个字符串数组,用于同时匹配多个头部条件。例如:`@RequestMapping(value = "/users", headers = "Content-Type=application/json")`。 5. consumes:指定处理方法可接受的请求内容类型。可以是一个字符串,用于匹配请求的Content-Type头部;或者是一个字符串数组,用于同时匹配多个内容类型。例如:`@RequestMapping(value = "/users", consumes = "application/json")`。 6. produces:指定处理方法可返回的响应内容类型。可以是一个字符串,用于设置返回的Content-Type头部;或者是一个字符串数组,用于同时设置多个内容类型。例如:`@RequestMapping(value = "/users", produces = "application/json")`。 上述参数可以组合使用,以更精确地映射请求,例如: ```java @RequestMapping(value = "/users", method = RequestMethod.GET, params = "id=1") public User getUserById() { // 处理方法逻辑 } ``` 在上述示例中,请求路径为`/users`,请求方法为GET,同时需要满足参数`id=1`的条件才能匹配到该处理方法。 通过合理使用@RequestMapping参数,我们可以灵活地映射和处理各种请求,并满足不同的业务需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值