springMVC中的注解@RequestParam与@PathVariable的区别

1、@PathVariable

@PathVariable是用来获得请求url中的动态参数的

@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。//配置url和方法的一个关系@RequestMapping(“item/{itemId}”)
@RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的action请求

@responsebody表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
比如异步获取json数据,加上@responsebody后,会直接返回json数据。

@Pathvariable注解绑定它传过来的值到方法的参数上
用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数

@ResponseBody
public TbItem getItemById(@PathVariable Long itemId){

@RequestMapping("/zyh/{type}")public String zyh(@PathVariable(value = "type") int type) throws UnsupportedEncodingException {String url = "http://wx.diyfintech.com/zyhMain/" + type;if (type != 1 && type != 2) {throw new IllegalArgumentException("参数错误");}String encodeUrl = URLEncoder.encode(url, "utf-8");String redirectUrl = MessageFormat.format(OAUTH_URL, WxConfig.zyhAppId, encodeUrl, "snsapi_userinfo", UUID.randomUUID().toString().replace("-", ""));return "redirect:" + redirectUrl;}

2、@RequestParam

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

这里主要讲这个注解 @RequestParam

接下来我们看一下@RequestParam注解主要有哪些参数:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

public List getItemTreeNode(@RequestParam(value=“id”,defaultValue=“0”)long parentId)

@Controller
@RequestMapping("/wx")
public class WxController {@Autowiredprivate WxService wxService;private static final Log log= LogFactory.getLog(WxController.class);@RequestMapping(value = "/service",method = RequestMethod.GET)public void acceptWxValid(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce,@RequestParam String echostr, HttpServletResponse response) throws IOException {PrintWriter out = response.getWriter();if (SignUtil.checkSignature(signature, timestamp, nonce)) {out.print(echostr);}elseout.print("fail");out.flush();out.close();}

@RequestParam与@PathVariable的区别相关推荐

  1. 传参:@RequestParam @RequestBody @PathVariable的作用

    @PathVariable 当使用@RequestMapping URI template 样式映射时,@PathVariable能使传过来的参数绑定到路由上,这样比较容易写出restful api, ...

  2. Spring MVC中@RequestParam和@PathVariable批注之间的区别?

    Spring MVC框架是在Java世界中开发Web应用程序最流行的框架之一,它还提供了一些有用的注释,可以从传入的请求中提取数据并将请求映射到控制器,例如@ RequestMapping,@ Req ...

  3. @RequestBody、 @RequestParam 、 @PathVariable 和 @Vaild 注解的使用及区别

    @RequestBody @RequestBody 主要用来接收前端传递给后端的 json 字符串中的数据(请求体中的数据): Get 方式无请求体,所以使用 @RequestBody 接收数据时,前 ...

  4. @RequestBody、 @RequestParam 、 @PathVariable 和 @Vaild 注解

    后端接收前端传的参数主要有两种方式: 第一种: 用变量来接收,前端传递的参数会对相同变量名进行赋值. 第(1)种: @GetMapping("/info/{id}")public ...

  5. postbody传参_post传参params与body的区别(@RequestParam和@RequestBody的区别)

    1.axios post请求  Content-Type默认为 application/x-www-form-urlencoded,我们传递参数的时,params里面的参数(简单的对象,通过 &quo ...

  6. 【spring学习笔记】(二)Spring MVC注解配置 参数转换注解@RequestMapping@RequestParam、@PathVariable@MatrixVariable

    @TOC 介绍 在Spring MVC项目中,<\context:component-scan>配置标签还会开启@Request-Mapping.@GetMapping等映射注解功能(也就 ...

  7. @RequestBody、@RequestParam、@PathVariable

    @RequestBody.@RequestParam.@PathVariable RequestParam 后端接收 可以设置默认值@RequestParam(value="currentp ...

  8. @requestparam @param @pathvariable @requestbody的区别

    @requestParam注解 用来获取前台传递过来的参数,例如获取以下链接的参数: http://api.nc.com/api/item/category/list?pid=0 public Str ...

  9. @RequestParam和@RequestBody的区别 (结合 Get/Post )

    一.get方法 请求:例如http://127.0.01:1025/download?url=https://www.baidu.com&token=opera-dev 获取请求参数: 直接获 ...

最新文章

  1. 平流式隔油池计算_当隔油池整改工作遇上“露天铁板烧”
  2. 测试多个线程调用同一静态方法(无静态变量)时是否有线程安全问题
  3. 如何取消计算机用户名,Win10如何取消登录界面显示用户名?
  4. 最新SpringBoot2.0X整合SpringData JPA实战完整篇
  5. 安装redis3.2.4集群时出现的一些坑
  6. 开源中国 精彩回顾_如何撰写精彩的事件回顾
  7. 排序算法-冒泡排序算法
  8. wos 文献被引_全世界最权威的文献检索工具,这6个检索技巧必须要掌握!
  9. iOS 蓝牙使用小结 bluetooth
  10. 华为又遭重击:谷歌暂停提供 Android 支持,新出售手机不能使用 Google 服务
  11. jQuery选择器--总结
  12. Intersecting Lines - POJ 1269(判断平面上两条直线的关系)
  13. macOS下安装ENVI
  14. uc3854 matlab仿真,基于UC3854硬开关PFC变换电路设计课程设计.doc
  15. 树莓派模拟电路_为树莓派开发板增加模拟信号接口
  16. iOS中物理单位换算
  17. 江恩 计算机,江恩计算器
  18. Tensor基础操作总结
  19. X-Powered-By: Servlet/3.0漏洞修复
  20. DSPE-PEG-GE11,磷脂-聚乙二醇-肿瘤细胞表皮生长因子肽GE11

热门文章

  1. EMNLP 2020 | 基于Wasserstein距离的正则化序列表示
  2. 知识表示与融入技术前沿进展及应用
  3. 岗位内推 | 腾讯云小微自然语言技术中心招聘NLP研究型实习生
  4. GELU的两个初等函数近似是怎么来的?
  5. SpringBoot 集成 druid 监控数据库报错 Failed to bind properties under ‘xxxx‘ to javax.sql.DataSource 解决(含配置源码)
  6. 前端参数无法转为后端实体内部类_Spring Boot返回前端Long型丢失精度
  7. Right-Left Cipher
  8. 2021年 第12届 蓝桥杯 第3次模拟赛真题详解及小结【Java版】
  9. PHP-计算表单提交的数据
  10. Android Gson的使用总结