【java】--@PathParam 和@QueryParam

参数(Parameter Annotations) 
Parameter Annotations用于获取client发送的数据。本文只介绍常用的注解,更多详见Jersey用户手册 
@PathParam 
使用@PathParam可以获取URI中指定规则的参数,比如:

@GET
@Path("{username"})
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("username") String userName) {
    ...
}

当浏览器请求http://localhost/user/jack时,userName值为jack。 

注意,这里的username并不是说Key是username, value是jack而是说/usr/后面跟着的东西就是username,这里username只是个变量

至于key,value的形式,下面也有

@QueryParam 
@QueryParam用于获取GET请求中的查询参数,如:

 


@GET
@Path("/user")
@Produces("text/plain")
public User getUser(@QueryParam("name") String name,
                    @QueryParam("age") int age) {
    ...
}

当浏览器请求http://host:port/user?name=rose&age=25时,name值为rose,age值为25。如果需要为参数设置默认值,可以使用@DefaultValue,如:


@GET
@Path("/user")
@Produces("text/plain")
public User getUser(@QueryParam("name") String name,
                    @DefaultValue("26") @QueryParam("age") int age) {
    ...
}

当浏览器请求http://host:port/user?name=rose时,name值为rose,age值为26。 

@FormParam 
@FormParam,顾名思义,从POST请求的表单参数中获取数据。如:

@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("name") String name) {
    // Store the message
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
@RequestParam和@PathVariable是Spring MVC中的注解,用于从请求中获取参数。 @RequestParam注解用于从请求的查询字符串中获取参数,并将其绑定到相应的方法参数上。可以通过设置value属性来指定参数名称,required属性来指定参数是否是必需的。例如,在@RequestMapping注解中使用@RequestParam注解,可以通过http://localhost:8080/springmvc/hello/101?param1=10&param2=20的URL来获取param1和param2参数的值。 @PathVariable注解用于从URL中的路径变量中获取参数,并将其绑定到相应的方法参数上。可以通过设置value属性来指定路径变量的名称。例如,通过@RequestMapping注解中使用@PathVariable注解,可以通过http://localhost:8080/springmvc/hello/101param1=10&param2=20的URL来获取id、param1和param2参数的值。 总结起来,@RequestParam注解从请求的查询字符串中获取参数,而@PathVariable注解从URL路径中获取参数。它们都用于接收请求中的参数,但关键的不同是@RequestParam从请求中获取值,而@PathVariable从URL模板中填充值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [@PathParam和@RequestParam的区别](https://blog.csdn.net/Captain249/article/details/90764616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值