温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringMVC中@RequestMapping参数如何使用

发布时间:2021-08-12 10:54:25 来源:亿速云 阅读:121 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关SpringMVC中@RequestMapping参数如何使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

@RequestMapping的参数和用法

RequestMapping里面的注解包含的参数如图:

SpringMVC中@RequestMapping参数如何使用

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

@RequestMapping 除了修饰方法, 还可来修饰类 :

类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录;

方法处:提供进一步的细分映射信息。 相对于类定义处的 URL。

若类定义处未标注 @RequestMapping,则方法处标记的 URL相对于 WEB 应用的根目录

返回ModelAndView时的url会根据你的 @RequestMapping实际情况组成。

如果类上没有映射,那么url直接就是方法的映射;否则url为类上+方法上映射路径组合。

对应项目jsp位置则是一级路径对应一级文件目录。

如url为/default/index对应项目中webapp/default/index.jsp

RequestMapping注解有六个属性,下面我们把她分成三类进行说明

【1】value, method

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

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

【2】consumes,produces

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

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

【3】params,headers

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

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

测试示例如下:

【1】value||path
  • JSP 页面

<a href="springmvc/testRequestMapping" rel="external nofollow" >Test RequestMapping</a>
  • controller

@RequestMapping(value="/testRequestMapping" )
    public String testRequestMapping() {
        System.out.println("testRequestMapping");
        return SUCCESS;
    }

成功返回success.jsp 。

Tips :若 href 属性值,不等于value值,则将提示404错误。

value的uri值为以下三类:

A) 可以指定为普通的具体值;

如下:

@RequestMapping("/testRequestMapping")

B) 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables)–restful风格;

@RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable Integer id2) {
        System.out.println("testPathVariable: " + id2);
        return SUCCESS;
    }

除了value还有path,二者效果等同,可以参考源码如下图:

SpringMVC中@RequestMapping参数如何使用

其中关于@PathVariable 有如下说明:

① 如果路径中的变量与方法中的变量名一致,可直接使用@PathVariable;

② 如果二者不一致,则使用@PathVariable(Variable)显示指定要绑定的路径中的变量 。

@PathVariable只能绑定路径中的占位符参数,且路径中必须有参数。

@PathVariable用法参考路径参数绑定参考

@RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") Integer id2) {
        System.out.println("testPathVariable: " + id2);
        return SUCCESS;
    }
    //路径中的 id 与 方法中的 id2 绑定

C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);

  @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")  
  public void handle(@PathVariable String version, @PathVariable String extension) {      
    // ...  
  }  
}
【2】method
  • JSP 页面

<a href="springmvc/testMethod" rel="external nofollow" >Test Method</a>
//href 默认为get 请求。
  • controller–限制接收post 请求。

@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    public String testMethod() {
        System.out.println("testMethod");
        return SUCCESS;
    }
  • result as follows :

HTTP Status 405 - Request method ‘GET' not supported 。

SpringMVC中@RequestMapping参数如何使用

【状态码405表示:请求中指定的方法不被允许。】

将method 改为method = RequestMethod.GET正常跳转页面。

【3】consumes
  • JSP 页面

仍以testMethod为例,提交表单。

默认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>

SpringMVC中@RequestMapping参数如何使用

  • controller–限制接收post 请求以及consumes="application/json"。

@RequestMapping(value = "/testMethod", method = RequestMethod.POST,consumes="application/json")
    public String testMethod() {
        System.out.println("testMethod");
        return SUCCESS;
    }
  • result as follows :

SpringMVC中@RequestMapping参数如何使用

【状态码415表示:由于媒介类型不被支持,服务器不会接受请求。。】

去掉 consumes属性,页面正常跳转 !

【4】produces

后台代码如下:

@RequestMapping(value = "/testMethod", method = RequestMethod.POST,produces="application/json")
    public void testMethod2(HttpServletRequest request,HttpServletResponse response,Model model) 
    throws IOException {
        request.getHeader("Accept");
        System.out.println(request.getHeader("Accept"));
//      response.setContentType("application/json");
        String username = request.getParameter("username");
        System.out.println("testMethod..."+username);
        model.addAttribute("user", username);
        Object jsonString =  "{'name': 'helloworlda'}";
        JSONObject jsonobj=JSONObject.fromObject(jsonString);
        PrintWriter out = response.getWriter();
        out.print(jsonobj);
    }
  • 浏览器请求头

text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

其中最后一项 : */*;q=0.8。

该项表明可以接收任何类型的,权重系数0.8表明如果前面几种类型不能正常接收。则使用该项进行自动分析。

application/json 几种主流浏览器都可以自动解析。

【5】params
  • JSP页面

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=tom;age = 10;

  • 后台代码:

设定必须包含username 和age两个参数,且age参数不为10 (可以有多个参数)。

@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" })
    public String testParamsAndHeaders() {
        System.out.println("testParamsAndHeaders");
        return SUCCESS;
    }
  • result as follows :

SpringMVC中@RequestMapping参数如何使用

【状态码400表示:服务器未能理解请求。 】

  • 将age 改为其他值,正常跳转。

SpringMVC中@RequestMapping参数如何使用

【6】headers
  • 浏览器请求头如下:

SpringMVC中@RequestMapping参数如何使用

  • 后台测试代码如下:

@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }, headers = { "Accept-Language=US,zh;q=0.8" })
    public String testParamsAndHeaders() {
        System.out.println("testParamsAndHeaders");
        return SUCCESS;
    }

设定请求头中第一语言必须为US。

  • result as follows :

SpringMVC中@RequestMapping参数如何使用

【状态码404表示:服务器无法找到被请求的页面。】

将后台代码改为zh-CN。。。

SpringMVC中@RequestMapping参数如何使用

页面正常跳转。

【Tips】:

① 服务器首先根据URL去找页面,如果找不到就返回404;

② 如果找到,但是不能正常处理,就会返回 5XX 类型错误。

看完上述内容,你们对SpringMVC中@RequestMapping参数如何使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI