Hello, I’m Chenxi. I’m glad you can read it. This article has compiled some usages of @PathVariable annotations, test them with cases, leave them to myself, share with beginners, share new knowledge, and make progress together!
Article Directory
1. Annotation
@PathVariable maps the placeholders bound to the URL
through @PathVariable, you can bind the placeholder parameters in the URL to the input parameters of the controller processing method: the {xxx} placeholder in the URL can be passed
@PathVariable("xxx") is bound to the input parameter of the operation method.
Generally used with @RequestMapping(method = RequestMethod.GET)
@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String name){
return userService.selectUser(name);
}
1. If the method parameter name is the same as the variable name in the URL that needs to be bound, it can be abbreviated:
@RequestMapping("/getUser/{name}")
public User getUser(@PathVariable String name){
return userService.selectUser(name);
}
2. If the method parameter name is inconsistent with the variable name in the URL that needs to be bound, write it as:
@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String userName){
return userService.selectUser(userName);
}
Two, code practice
We use postman to test /test/chenxi
Our controller layer receives the value and prints it out and finds it can be printed out
At this time, we removed @PathVariable and found that the postman test found that the value was empty, that is, the parameter in the placeholder could not be obtained
I believe you must understand...
The best investment is to invest in yourself.
2020.09.26 May you all go to be in your love!