@PathVariable("ownerId") String theOwner 和@PathVariable String theOwner 的区别
Difference between @PathVariable("ownerId") String theOwner and @PathVariable String theOwner
@PathVariable("ownerId") String theOwner
和Spring MVC
中的@PathVariable String theOwner
有区别吗?
我已经完成了 Spring @PathVariable,但我不清楚这个概念。
我通过分析 uri parameter
是否与 variable name
具有相同的名称发现了一个区别,那么您可以直接将其存储在变量中。这是否正确,或者您可以 post 与此相关的任何其他信息。
@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
}
唯一的区别是第一个选项允许您更改方法中的参数名称,您可以使用 theOwner
而不是 ownerId
。
这可能很有用,例如,如果您有一个 class 成员同名 ownerId
来自您提到的文档页面:
The URI Template "/owners/{ownerId}" specifies the variable name `ownerId. When the controller handles this request, the value of ownerId is set to the value found in the appropriate part of the URI. For example, when a request comes in for /owners/fred, the value of ownerId is fred.
因此,如果您不想指定 @PathVariable 的值,请在您的方法参数和路径模板中使用相同的名称。
Spring MVC documentation 非常清楚它是如何工作的:
To process the @PathVariable
annotation, Spring MVC needs to find the matching URI template variable by name. You can specify it in the annotation:
@GetMapping("/owners/{ownerId}")
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}
Or if the URI template variable name matches the method argument name you can omit that detail. As long as your code is compiled with debugging information or the -parameters
compiler flag on Java 8, Spring MVC will match the method argument name to the URI template variable name:
@GetMapping("/owners/{ownerId}")
public String findOwner(@PathVariable String ownerId, Model model) {
// implementation omitted
}
@PathVariable("ownerId") String theOwner
和Spring MVC
中的@PathVariable String theOwner
有区别吗?
我已经完成了 Spring @PathVariable,但我不清楚这个概念。
我通过分析 uri parameter
是否与 variable name
具有相同的名称发现了一个区别,那么您可以直接将其存储在变量中。这是否正确,或者您可以 post 与此相关的任何其他信息。
@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
}
唯一的区别是第一个选项允许您更改方法中的参数名称,您可以使用 theOwner
而不是 ownerId
。
这可能很有用,例如,如果您有一个 class 成员同名 ownerId
来自您提到的文档页面:
The URI Template "/owners/{ownerId}" specifies the variable name `ownerId. When the controller handles this request, the value of ownerId is set to the value found in the appropriate part of the URI. For example, when a request comes in for /owners/fred, the value of ownerId is fred.
因此,如果您不想指定 @PathVariable 的值,请在您的方法参数和路径模板中使用相同的名称。
Spring MVC documentation 非常清楚它是如何工作的:
To process the
@PathVariable
annotation, Spring MVC needs to find the matching URI template variable by name. You can specify it in the annotation:@GetMapping("/owners/{ownerId}") public String findOwner(@PathVariable("ownerId") String theOwner, Model model) { // implementation omitted }
Or if the URI template variable name matches the method argument name you can omit that detail. As long as your code is compiled with debugging information or the
-parameters
compiler flag on Java 8, Spring MVC will match the method argument name to the URI template variable name:@GetMapping("/owners/{ownerId}") public String findOwner(@PathVariable String ownerId, Model model) { // implementation omitted }