Search code examples
javaspringspring-mvcpath-variables

Difference between @PathVariable("ownerId") String theOwner and @PathVariable String theOwner


is there any difference between @PathVariable("ownerId") String theOwner and @PathVariable String theOwner in Spring MVC.

I've gone through with the Spring @PathVariable, but I'm not clear about the concept.

I found one difference by analyzing if the uri parameter is having the same name as the variable name , then you can directly store it in variable. Is this correct or you can post any other information related to this.

@GetMapping("/owners/{ownerId}")
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}

@GetMapping("/owners/{ownerId}")
public String findOwner(@PathVariable String ownerId, Model model) {
// implementation omitted
}

Solution

  • The only difference is that first option allow you to change parameter name in your method, you can use theOwner and not ownerId.

    This can be useful for example if you have a class member with same name ownerId