@PathVariable只支持一个属性value,类型是为String,代表绑定的属性名称。默认不传递时,绑定为同名的形参。

用来便捷地提取URL中的动态参数。其英文注释如下:

Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for {@link RequestMapping} annotated handler methods in Servlet environments.

应用时,在@RequestMapping请求路径中,将需要传递的参数用花括号{}括起来,然后,通过@PathVariable("参数名称")获取URL中对应的参数值。如果@PathVariable标明参数名称,则参数名称必须和URL中参数名称一致。

@RequestMapping("/viewUser/{id}/{name}")
public Map<String, Object> viewUser(@PathVariable("id") Integer idInt, @PathVariable Integer name) {
System.out.println("@PathVariable中 请求参数 id = " + idInt);
Map<String, Object> user = new HashMap<>();
user.put("id", idInt);
user.put("name", name);
return user;
}
/**
* @Title viewUser2
* @Description @PathVariable未标注参数名称,则被注解参数名必须后URL中的一致
* @date 2018-12-15 11:08
*/
@RequestMapping("/owners/{ownerId}")
public Map<String, Object> viewUser2(@PathVariable Integer ownerId) {
System.out.println("@PathVariable中 请求参数 ownerId = " + ownerId);
Map<String, Object> user = new HashMap<>();
user.put("id", ownerId);
user.put("name", "Lucy");
return user;
}

URI 模板 “/owners/{ownerId}” 指定了一个名叫 ownerId的变量。当控制器处理这个请求时,ownerId的值被设置为从 URI 中解析出来。比如,当请求 /viewUser/100 进来时,100 就是 ownerId的值。

参考文献:https://docs.spring.io/spring/docs/5.0.0.M1/spring-framework-reference/htmlsingle/#mvc-ann-requestmapping-uri-templates

注解之@PathVariable的更多相关文章

  1. 【spring boot】注解@ApiParam @PathVariable @RequestParam三者区别

    1.@ApiParam,就是用于swagger提供开发者文档,文档中生成的注释内容. @ApiOperation( value = "编辑公告", notes = "编辑 ...

  2. 阶段3 3.SpringMVC·_03.SpringMVC常用注解_3 PathVariable注解

    请求地址都一样,根据不同的请求方式,最终让不同的方法去执行.这就是restfull的风格 如果有两个查询都是get的.那么当前访问 满足条件的就是两个. 用一个占位符 {id} 演示PathVaria ...

  3. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  4. SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解

    request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...

  5. 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...

  6. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  7. (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  8. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  9. Spring MVC常用注解@PathVariable、@RequestHeader、@CookieValue、@RequestParam、@RequestBody、@SessionAttributes、@ModelAttribute

    简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri部分(这里指uri template中 ...

随机推荐

  1. CTimeSpan

    要获取两个时间差,如两个CTime的时间差,可以使用MFC中的CTimeSpan类. CTime time1 = CTime::GetCurrentTime(); CTime time2 = CTim ...

  2. Python 的 Magic Methods 指南(转)

    介绍 本指南是数月博客的总结.主题是魔术方法. 什么是魔术方法呢?它们是面向对象Python语言中的一切.它们是你可以自定义并添加“魔法”到类中的特殊方法.它们被双下划线环绕(比如__init__或_ ...

  3. [Tensorflow] RNN - 03. MultiRNNCell for Digit Prediction

    Ref: http://blog.csdn.net/u014595019/article/details/52759104 Time: 2min Successfully downloaded tra ...

  4. 【应用安全】S-SDLC安全开发生命周期

    0x01 S-SDLC简介 OWASP Secure Software Development Lifecycle Project(S-SDLC)是OWASP组织首个由OWASP中国团队独立发布并主导 ...

  5. Automatic Summarization of Bug Reports

      CONTENT: example : KDE bug report: https://bugs.kde.org/show_bug.cgi?id=188311 (其中还有很多comments没显示) ...

  6. POJ 1661 Help Jimmy(DP/最短路)

    Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14980 Accepted: 4993 Descripti ...

  7. WinForm资源管理器开发(TreeView&ListView)

    在C# WinForm开发当中,有三大View控件值得深入应用,分别为DataGridView.ListView.TreeView.如果这三大控件能够熟练的应用,其它的控件也就基本没有问题.所以这篇博 ...

  8. GDI+绘制五星红旗

    五星红旗是由红色背景,加5个黄色五角星组成.绘制一个五星红旗的思路,就是先定义一个五角星的自定义控件,然后通过设置五角星的大小.位置.旋转角度等属性,组合成一个五星红旗. 五角星自定义控件代码: pu ...

  9. ftp如何使用命令上传文件

    本地上传到服务器的步骤如下: 1."开始"-"运行"-输入"FTP" 2.open qint.ithot.net 这一步可以与第一步合并,在 ...

  10. Java 文件读写操作

    1[1]按字节读写,一次只读取一个字节,效率比较低 package bk1; import java.io.File; import java.io.FileInputStream; import j ...