转自:http://blog.csdn.net/u011410529/article/details/66974974

@RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam是从request里面拿取值,而 @PathVariable 是从一个URI模板里面来填充

@RequestParam

看下面一段代码:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

根据上面的这个URL,你可以用这样的方式来进行获取

public String getDetails(
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2){
...
}

@RequestParam 支持下面四种参数

  • defaultValue 如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值
  • name 绑定本次参数的名称,要跟URL上面的一样
  • required 这个参数是不是必须的
  • value 跟name一样的作用,是name属性的一个别名

@PathVariable

这个注解能够识别URL里面的一个模板,我们看下面的一个URL

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

上面的一个url你可以这样写:

@RequestMapping("/hello/{id}")
public String getDetails(@PathVariable(value="id") String id,
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2){
.......
}

区别很明显了

@PathParam

这个注解是和spring的pathVariable是一样的,也是基于模板的,但是这个是jboss包下面的一个实现,上面的是spring的一个实现,都要导包

@QueryParam

@QueryParam 是 JAX-RS 本来就提供的,和Spring的RequestParam作用一致

@ResponseBody

responseBody表示服务器返回的时候以一种什么样的方式进行返回, 将内容或对象作为 HTTP 响应正文返回,值有很多,一般设定为json

@RequestBody

一般是post请求的时候才会使用这个请求,把参数丢在requestbody里面

转@RequestParam,@PathParam,@PathVariable等注解区别的更多相关文章

  1. @RequestParam,@PathVariable等注解区别

    一.@RequestParam和@PathVariable的区别 1.@RequestParam是从uri中request后面的参数串来取得参数的 2.@PathVariable是从uri模板中取得参 ...

  2. Restful中 @RequestParam,@PathParam,@PathVariable等注解区别

    @RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @P ...

  3. @RequestParam,@PathParam,@PathVariable等注解区别

    @RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @P ...

  4. @RequestParam 和 @ PathVariable 的区别

    @RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...

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

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

  6. @RequestParam和@PathVariable的区别及其应用场景

    @RequestParam和@PathVariable这两者之间区别不大,主要是请求的URL不一样 用@RequestParam请求接口时,URL是:http://www.test.com/user/ ...

  7. 浅谈 @RequestParam 和@PathVariable

    版权声明:本文为博主原创文章,如果对你有用,敬请带走! https://blog.csdn.net/chuck_kui/article/details/55506723 首先 上两个地址: 地址①ht ...

  8. [转帖]java注解与注释注解区别

    https://baijiahao.baidu.com/s?id=1615942718081024481&wfr=spider&for=pc 还需要仔细看一下书的 书里面都有. jav ...

  9. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

随机推荐

  1. xml 创建 和 处理 及其修改

    #创建xml import xml.etree.ElementTree as ET new_xml = ET.Element('namelist') personinfo = ET.SubElemen ...

  2. 使用Eclipse对FFmpeg进行调试

    在研究代码的过程中,调试运行是一种非常有效的方法.我们常用的Visual Studio建立的工程可以很方便地对程序进行调试运行.但是对于FFMpeg这样的工程,想要进行单步调试就没这么容易了.如果一定 ...

  3. 快速可靠网络传输协议 KCP(转)

    KCP 是一个快速可靠协议,能以比 TCP浪费10%-20%的带宽的代价,换取平均延迟降低30%-40%,且最大延迟降低三倍的传输效果.纯算法实现,并不负责底层协议(如UDP)的收发,需要使用者自己定 ...

  4. CPU Meltdown和Spectre漏洞分析

    一.背景: 1月4日,国外爆出了整个一代处理器都存在的灾难性漏洞:Meltdown和Spectre. 几乎影响了全球20年内所有cpu处理器:这两个漏洞可以使攻击者通过利用并行运行进程的方式来破坏处理 ...

  5. python 本地变量和全局变量 locals() globals() global nonlocal 闭包 以及和 scala 闭包的区别

    最近看 scala ,看到了它的作用域,特此回顾一下python的变量作用域问题. A = 10 B = 100 print A #10 print globals() #{'A': 10, 'B': ...

  6. textbox显示定位到最后一行(最新一行)

    this.textBox1.Select(this.txtMsgInfo.TextLength, 0); this.textBox1.ScrollToCaret();

  7. hive 抽样方法

    select * from (select *from advert.dws_advert_order_model_sample_pcvr_v2_diwhere dt>= date_sub('$ ...

  8. Installation failed with message INSTALL_CANCELED_BY_USER.

    Installation failed with message INSTALL_CANCELED_BY_USER. It is possible that this issue is resolve ...

  9. SpringCloud——Eureka服务注册和发现

    一.SpringCloud和Dubbo SpringCloud整合了一套较为完整的微服务解决方案框架,而Dubbo只是解决了微服务的几个方面的问题. content Dubbo SpringCloud ...

  10. express中使用ejs

    [express中使用ejs] 1.添加 ejs 依赖. npm install ejs --save 2.设置 view engine 为 ejs 即可.