Analysis of PathVariable annotation in Spring
- 2021-12-04 10:12:34
- OfStack
PathVariable annotation for Spring
Like @ RequestParam1, @ PathVariable annotations are used to extract data from HTTP request. However, they are slightly different. The difference is that @ RequestParam takes parameters from URL, while @ PathVariable just takes parameters from URI.
Example
Let's assume that you have one of the following URL sites:
http://www.yourwebsite.net/employee/1
The 1 in URL above represents the employee's ID. So far, 1 cut ok. But now, from the Spring controller, the path looks like this (depending on the name you give id):
/employee/{id}
How does the URL path above help us? Because of this {} syntax (which happens to be called the URI template), you can now pass it to a method using @ pathvariable, and your method and comments look like this:
@RequestMapping(value="/employee/{id}", method=RequestMethod.GET)
<Access Modifier> <Return Type> <Method Name> (@PathVariable <Type> id) { <body> }
As you can see from the above code snippet, "id" will now reference {id} from the path. Let's try it with a real example:
@Controller
@SpringBootApplication
public class EmployeeManager {
@RequestMapping(value=" /employee/{id}")
public String pathVariableDemo(Model model, @PathVariable int id) {
model.addAttribute("id", id);
return "demo";
}
}
Now, Spring looks at the output id parameter and matches it with the template variable "id".
Remember, if my code uses "id" (parameter) to name something else, it won't work. However, if you don't want to use the same name for parameters and template variables, you can name the PathVariable annotation like this:
@Controller
@SpringBootApplication
public class EmployeeManager {
@RequestMapping(value=" /employee/{id}")
public String pathVariableDemo(Model model, @PathVariable("id") int someRandomName) {
model.addAttribute("id", someRandomName);
return "demo";
}
}
As you can see from the above example, I changed the name of the parameter to _someRandomName, _ but also added @ PathVariable ("id"), which again specifies the template variable we want to reference.
Ultimately, you have two choices:
1. Use the same name for method parameters 2. Specify the name of the template variable in the @ PathVariable annotation
Multiple @ PathVariable annotations
What if you could have multiple @ pathvariable? Can we do that? Yes, we can! In fact, this is very similar to adding 1 @ pathvariable.
Let's demonstrate 1:
@Controller
@SpringBootApplication
public class EmployeeManager {
@RequestMapping(value="/{company}/employee/{id}", method=RequestMethod.GET)
public String pathVariableDemo(@PathVariable("company") String companyName, @PathVariable("id") int employeeId) {
// handle the code
return "demo";
}
}
As you can see from the above code snippet, we have two template variables:
company
id
We then extract each template variable and "assign" them to the method parameters by specifying the temporary variables referenced by the method parameters.
@ PathVariable vs @ RequestParam Conclusion
Although @ PathVariable and @ RequestParam are both used to extract values from URL, they can be used according to the design of URL.
Typically, @ PathVariable is typically used in the RESTful Web service, while @ RequestParam is used to extract data from query parameters.
Pits using @ PathVariable
Find a problem
The front-end path splices the user name, because the user name has the character '.' Similar to 'Oliver. wang'. The front-end browser F12 finds that the path splices normally 'xxxxxx/Oliver. wang? xxx=xxx', and the back-end code finds that the parameters of controller layer receive only 'Oliver'.
Solving process
Adding an extra parameter (xxx, HttpServletRequest request) {} in the method, the output request. ServletPath () shows that the path is normal '/xxx/xxx/Oliver. wang'.