广告
返回顶部
首页 > 资讯 > 精选 >使用@PathVariable注解怎么让spring支持参数带值功能
  • 539
分享到

使用@PathVariable注解怎么让spring支持参数带值功能

2023-06-06 12:06:45 539人浏览 独家记忆
摘要

本篇文章给大家分享的是有关使用@PathVariable注解怎么让spring支持参数带值功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。@PathVariable的作用获取

本篇文章给大家分享的是有关使用@PathVariable注解怎么让spring支持参数带值功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

@PathVariable的作用

获取URL动态变量,例如

  @RequestMapping("/users/{userid}")  @ResponseBody  public String getUser(@PathVariable String userid){    return "userid=" + userid;   }

@PathVariable的包引用

spring自从3.0版本就引入了org.springframework.WEB.bind.annotation.PathVariable,

这是RESTful一个具有里程碑的方式,将springMVC的精华推向了高潮,那个时代,跟微信公众号结合的开发如火如荼,很多东西都会用到URL参数带值的功能。

@PathVariable的PathVariable官方doc解释

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

- If the method parameter is Map<String, String> or MultiValueMap<String, String> then the map is populated with all path variable names and values.

翻译过来就是:

- 在Springmvc中可以使用@PathVariable注解,来支持绑定URL模板参数(占位符参数/参数带值)

- 另外如果controller的参数是Map(String, String)或者MultiValueMap(String, String),也会顺带把@PathVariable的参数也接收进去

@PathVariable的RESTful示范

前面讲作用的时候已经有一个,现在再提供多一个,别人访问的时候可以Http://localhost:8080/call/窗口号-检查编号-1

  @PutMapping("/call/{checkWicket}-{checkNum}-{status}")  public apiReturnObject call(@PathVariable("checkWicket") String checkWicket,@PathVariable("checkNum") String checkNum,      @PathVariable("status") String status) {    if(StringUtils.isBlank(checkWicket) || StringUtils.isBlank(checkNum)) {      return ApiReturnUtil.error("叫号失败,窗口号,检查者编号不能为空");    }else {      if(StringUtils.isBlank(status)) status ="1";      try {        lineService.updateCall(checkWicket,checkNum,status);        return ApiReturnUtil.success("叫号成功");      } catch (Exception e) {        return ApiReturnUtil.error(e.getMessage());      }    }  }

补充:解决@PathVariable接收参数带点号时只截取点号前的数据的问题

问题:

@RequestMapping(value = "preview/{fileName}", method = RequestMethod.GET)public void previewFile(@PathVariable("fileName") String fileName, httpservletRequest req, HttpServletResponse res) { officeOnlinePreviewService.previewFile(fileName, req, res);}

本来fileName参数传的是:userinfo.docx,

但结果接收到的是:userinfo

这显然不是我想要的。

解决方法:

@RequestMapping(value = "preview/{fileName:.+}", method = RequestMethod.GET)public void previewFile(@PathVariable("fileName") String fileName, HttpServletRequest req, HttpServletResponse res) { officeOnlinePreviewService.previewFile(fileName, req, res);}

参数fileName这样写,表示任何点(包括最后一个点)都将被视为参数的一部分:

{fileName:.+}

以上就是使用@PathVariable注解怎么让spring支持参数带值功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。

--结束END--

本文标题: 使用@PathVariable注解怎么让spring支持参数带值功能

本文链接: https://www.lsjlt.com/news/246891.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 使用@PathVariable注解怎么让spring支持参数带值功能
    本篇文章给大家分享的是有关使用@PathVariable注解怎么让spring支持参数带值功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。@PathVariable的作用获取...
    99+
    2023-06-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作