@ PathVariable annotation case of spring supporting parameter with value function
- 2021-08-28 20:07:32
- OfStack
The role of @ PathVariable
Gets an URL dynamic variable, such as
@RequestMapping("/users/{userid}")
@ResponseBody
public String getUser(@PathVariable String userid){
return "userid=" + userid;
}
Package references for @ PathVariable
spring has introduced org. springframework. web. bind. annotation. PathVariable since version 3.0,
This is a milestone way of RESTful1, which pushes the essence of springMVC to a climax. At that time, the development combined with WeChat official account was in full swing, and many things would use the function of URL parameter with value.
@ PathVariable's PathVariable official doc explanation
- 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.
Translated as:
-You can use the @ PathVariable annotation in SpringMVC to support binding URL template parameters (placeholder parameters/parameters with values)
-In addition, if the parameter of controller is Map (String, String) or MultiValueMap (String, String), the parameter of @ PathVariable will also be received incidentally
RESTful demonstration of @ PathVariable
There was already one when I talked about the function, and now I will provide one more one. When others visit, they can use http://localhost: 8080/call/Window Number-Check Number-1
/**
* Call a number
*/
@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(" Calling failed , Window number , Inspector number cannot be blank ");
}else {
if(StringUtils.isBlank(status)) status ="1";
try {
lineService.updateCall(checkWicket,checkNum,status);
return ApiReturnUtil.success(" Successful calling ");
} catch (Exception e) {
return ApiReturnUtil.error(e.getMessage());
}
}
}
Supplement: Solve the problem that @ PathVariable only intercepts the data before the dot number when receiving parameters with dot numbers
Question:
@RequestMapping(value = "preview/{fileName}", method = RequestMethod.GET)
public void previewFile(@PathVariable("fileName") String fileName, HttpServletRequest req, HttpServletResponse res) {
officeOnlinePreviewService.previewFile(fileName, req, res);
}
The original fileName parameter is: userinfo. docx,
But the result is: userinfo
This is obviously not what I want.
Solution:
@RequestMapping(value = "preview/{fileName:.+}", method = RequestMethod.GET)
public void previewFile(@PathVariable("fileName") String fileName, HttpServletRequest req, HttpServletResponse res) {
officeOnlinePreviewService.previewFile(fileName, req, res);
}
The parameter fileName is written to indicate that any point (including the last one) will be treated as part 1 of the parameter:
{fileName:.+}