zoukankan      html  css  js  c++  java
  • springMvc常用注解之@RequestMapping

    @RequestMapping的匹配规则

    • ?:匹配文件名中的一个字符
    • *:匹配文件名中的任意字符
    • **: ** 匹配多层路径
     /user/*/createUser: 匹配
    示例:/user/aaa/createUser、 /user/bbb/createUser 等 URL
    
    /user/**/createUser: 匹配
    示例:/user/createUser、 /user/aaa/bbb/createUser 等 URL
    
    /user/createUser??: 匹配
    示例:/user/createUseraa、 /user/createUserbb 等 URL

    @requestMapping中有六个参数,分别介绍如下:

    •  value(path), method;

      value:指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);

      method:指定请求的method类型, GET、POST、PUT、DELETE等;

    //这是最基础的使用
    @RequestMapping(value="/testRequestMapping" )
    public String testRequestMapping() {
        System.out.println("testRequestMapping");
        return SUCCESS;
    }
    
    //如果路径中的变量与方法中的变量名一致,可直接使用@PathVariable;如果二者不一致,则使用@PathVariable(Variable)显示指定要绑定的路径中的变量 ;PathVariable只能绑定路径中的占位符参数,且路径中必须有参数
    @ResponseBody
    @RequestMapping("/user/{username}/blog/{blogIdNum}")
    public String getUerBlog(@PathVariable String username , @PathVariable("blogIdNum") int blogId) {
        return "user: " + username + "blog->" + blogId;
    }
    //userName中只能包含小写字母,大写字母,数字,不合法的URL则不会被处理,直接由SpringMVC框架返回404 Not Found。 @RequestMapping("/user/{username:[a-zA-Z0-9_]+}/blog/{blogId}") public String testPathVariable(@PathVariable("id") Integer id2) { System.out.println("testPathVariable: " + id2); return SUCCESS; } //method的使用比较简单 @RequestMapping(value = "/testMethod", method = RequestMethod.POST) public String testMethod() { System.out.println("testMethod"); return SUCCESS; }
    •  consumes,produces;

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

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

    //例如我们提交表单,默认contentType为Content-Type:application/x-www-form-urlencoded。
    <form action="springmvc/testMethod" method="POST">
            <input type="text" name="username" value=""/>
            <input type="submit" value="submit"/>
        </form>
    //如果我们在请求接口@RequestMapping中使用了consumes="application/json"
    @RequestMapping(value = "/testMethod", method = RequestMethod.POST,consumes="application/json")
    public String testMethod() {
          System.out.println("testMethod");
          return SUCCESS;
    }
    //结果就会出现状态码415错误,表示:由于媒介类型不被支持,服务器不会接受请求。如果去掉consumes设置就可以正常的跳转
    //方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;
    @Controller
    @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
    @ResponseBody
    public Pet getPet(@PathVariable String petId, Model model) {    
        // implementation omitted
    }
    •  params,headers;

      params: 指定request中必须包含某些参数值是,才让该方法处理。

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

    //例如,我们提交表单
    <form action="springmvc/testParamsAndHeaders" method="POST">
            <input type="text" name="username" value=""/>
            <input type="text" name="age" value=""/>
            <input type="submit" value="submit"/>
        </form>
    //在方法上我们设定必须包含username 和age两个参数,且age参数不为10 (可以有多个参数)
    @RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" })
    public String testParamsAndHeaders() {
          System.out.println("testParamsAndHeaders");
          return SUCCESS;
    }
    //提交的参数符合我们限定的要求的情况下,就会返回状态码400,表示:服务器未能理解请求。
    //设置请求头中的相关参数
    @RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }, headers = { "Accept-Language=US,zh;q=0.8" })
    public String testParamsAndHeaders() {
          System.out.println("testParamsAndHeaders");
          return SUCCESS;
    }
    
    //如果请求头中的相关参数不符合设定就会返回404状态码

     

  • 相关阅读:
    JAVA动态代理学习
    .Netcore Swagger
    无废话,用.net core mvc 开发一个虽小但五脏俱全的网站
    专为开发者开发的导航网站
    利用webbrowser自动查取地点坐标
    帮你理解学习lambda式
    activeX 打包
    activeX 开发
    提取验证码到winform上webbroswer和axwebbroswer
    存储过程分页的注入问题以及解决
  • 原文地址:https://www.cnblogs.com/htyj/p/11937462.html
Copyright © 2011-2022 走看看