@PathVariable注解,讓spring支持參數(shù)帶值功能的案例
@PathVariable的作用
獲取URL動態(tài)變量,例如
@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的精華推向了高潮,那個時代,跟微信公眾號結(jié)合的開發(fā)如火如荼,很多東西都會用到URL參數(shù)帶值的功能。
@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模板參數(shù)(占位符參數(shù)/參數(shù)帶值)
- 另外如果controller的參數(shù)是Map(String, String)或者M(jìn)ultiValueMap(String, String),也會順帶把@PathVariable的參數(shù)也接收進(jìn)去
@PathVariable的RESTful示范
前面講作用的時候已經(jīng)有一個,現(xiàn)在再提供多一個,別人訪問的時候可以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接收參數(shù)帶點號時只截取點號前的數(shù)據(jù)的問題
問題:
@RequestMapping(value = "preview/{fileName}", method = RequestMethod.GET) public void previewFile(@PathVariable("fileName") String fileName, HttpServletRequest req, HttpServletResponse res) { officeOnlinePreviewService.previewFile(fileName, req, res); }
本來fileName參數(shù)傳的是:userinfo.docx,
但結(jié)果接收到的是:userinfo
這顯然不是我想要的。
解決方法:
@RequestMapping(value = "preview/{fileName:.+}", method = RequestMethod.GET) public void previewFile(@PathVariable("fileName") String fileName, HttpServletRequest req, HttpServletResponse res) { officeOnlinePreviewService.previewFile(fileName, req, res); }
參數(shù)fileName這樣寫,表示任何點(包括最后一個點)都將被視為參數(shù)的一部分:
{fileName:.+}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
SpringBoot獲取http數(shù)據(jù)、打印HTTP參數(shù)的4種方式
Java的話本地打斷點可以調(diào)試獲取rest入?yún)?但是在生產(chǎn)環(huán)境可能我們獲取入?yún)ⅲ℉ttp?header/parameter)可能就沒有那么的輕松了,所以本文給大家介紹了SpringBoot獲取http數(shù)據(jù)、打印HTTP參數(shù)的4種方式,需要的朋友可以參考下2024-03-03Java 網(wǎng)絡(luò)編程socket編程等詳解
本篇文章主要介紹了java網(wǎng)絡(luò)編程中的類的方法以及實例,需要的朋友可以參考下2017-04-04Java整合mybatis實現(xiàn)過濾數(shù)據(jù)
這篇文章主要介紹了Java整合mybatis實現(xiàn)過濾數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01java中使用Filter控制用戶登錄權(quán)限具體實例
java中使用Filter控制用戶登錄權(quán)限具體實例,需要的朋友可以參考一下2013-06-06ConditionalOnProperty注解的作用和使用方式
在SpringBoot項目開發(fā)中,@ConditionalOnProperty注解允許根據(jù)配置文件中的屬性值來控制配置類是否生效,該注解通過屬性name和havingValue來判斷配置是否注入,如果application.properties中的對應(yīng)屬性值為空或不匹配havingValue設(shè)定值2024-09-09