@PathVariable注解,讓spring支持參數(shù)帶值功能的案例
@PathVariable的作用
獲取URL動(dòng)態(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一個(gè)具有里程碑的方式,將springMVC的精華推向了高潮,那個(gè)時(shí)代,跟微信公眾號(hào)結(jié)合的開(kāi)發(fā)如火如荼,很多東西都會(huì)用到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.
翻譯過(guò)來(lái)就是:
- 在SpringMVC中可以使用@PathVariable注解,來(lái)支持綁定URL模板參數(shù)(占位符參數(shù)/參數(shù)帶值)
- 另外如果controller的參數(shù)是Map(String, String)或者M(jìn)ultiValueMap(String, String),也會(huì)順帶把@PathVariable的參數(shù)也接收進(jìn)去
@PathVariable的RESTful示范
前面講作用的時(shí)候已經(jīng)有一個(gè),現(xiàn)在再提供多一個(gè),別人訪(fǎng)問(wèn)的時(shí)候可以http://localhost:8080/call/窗口號(hào)-檢查編號(hào)-1
/**
* 叫號(hào)
*/
@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("叫號(hào)失敗,窗口號(hào),檢查者編號(hào)不能為空");
}else {
if(StringUtils.isBlank(status)) status ="1";
try {
lineService.updateCall(checkWicket,checkNum,status);
return ApiReturnUtil.success("叫號(hào)成功");
} catch (Exception e) {
return ApiReturnUtil.error(e.getMessage());
}
}
}
補(bǔ)充:解決@PathVariable接收參數(shù)帶點(diǎn)號(hào)時(shí)只截取點(diǎn)號(hào)前的數(shù)據(jù)的問(wèn)題
問(wèn)題:
@RequestMapping(value = "preview/{fileName}", method = RequestMethod.GET)
public void previewFile(@PathVariable("fileName") String fileName, HttpServletRequest req, HttpServletResponse res) {
officeOnlinePreviewService.previewFile(fileName, req, res);
}
本來(lái)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這樣寫(xiě),表示任何點(diǎn)(包括最后一個(gè)點(diǎn))都將被視為參數(shù)的一部分:
{fileName:.+}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
SpringBoot通過(guò)tractId操作日志鏈路跟蹤
這篇文章給大家介紹SpringBoot通過(guò)tractId操作日志鏈路跟蹤,通過(guò)tractId,即可完成對(duì)從一個(gè)請(qǐng)求進(jìn)入系統(tǒng)到請(qǐng)求結(jié)束的日志追蹤,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10
Mybatis輸入輸出映射及動(dòng)態(tài)SQL Review
這篇文章主要介紹了Mybatis輸入輸出映射及動(dòng)態(tài)SQL Review,需要的朋友可以參考下2017-02-02
SpringMVC學(xué)習(xí)之JSON和全局異常處理詳解
在項(xiàng)目上線(xiàn)之后,往往會(huì)出現(xiàn)一些不可預(yù)料的異常信息,對(duì)于邏輯性或設(shè)計(jì)性問(wèn)題,開(kāi)發(fā)人員或者維護(hù)人員需要通過(guò)日志,查看異常信息并排除異常,這篇文章主要給大家介紹了關(guān)于SpringMVC學(xué)習(xí)之JSON和全局異常處理的相關(guān)資料,需要的朋友可以參考下2022-10-10
SpringBoot配置文件導(dǎo)入方法詳細(xì)講解
Spring Boot雖然是Spring的衍生物, 但默認(rèn)情況下Boot是不能直接使用Spring的配置文件的, 我們可以通過(guò)兩種方式導(dǎo)入Spring的配置2022-10-10
數(shù)組在java中的擴(kuò)容的實(shí)例方法
在本篇文章里小編給大家分享的是一篇關(guān)于數(shù)組在java中的擴(kuò)容的實(shí)例方法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01

