In this quick tutorial, we'll check out Prince @PathVariable's annotation.
Simply put, the @PathVariable annotation can manage the dynamic template on the URI application map and set it as path parameters.
Let's see how we use @PathVariable and its various features.
An easy-to-use case for @PathVariable annotation may be the last place to identify a business with a key:
@GetMapping ("/ api / staff / {id}")
@ResponseBody
public String getEmployeesById (@PathVariable String id) {
return "ID:" + id;
}
In this example, we use the @PathVariable annotation to extract an URI template,
represented by the variable { id } .
A simple GET request to / api / employees / {id} will use GetEmployeesById with the issued id value:
http: // localhost: 8080 / api / staff / 111
----
Identity: 111
Now let's take a closer look at this annotation, and see what its features are.
In the previous example, we skipped the definition of a variable template method as the parameters of the method parameter, and the variability of the method was the same.
However, if the variable name for the method is different,
we can define it in the @PathVariable annotation argument:
@GetMapping ("/ api / workerswithvariable / {id}")
@ResponseBody
public String getEmployeesByIdWithVariableName (@PathVariable ("id") String employeeId)
return "ID:" + employeeId;
}
http: // localhost: 8080 / api / workerswithvariable / 1
----
Identity: 1
We can also define a variable name for the method as @PathVariable (value = ”id”) instead of PathVariable (“id”) for clarification.
Depending on the usage case, we may have more than one method in our URI control system application, with multiple method parameters:
@GetMapping ("/ api / workers / {id} / {name}")
@ResponseBody
public String getEmployeesByIdAndName (@PathVariable String id,
@PathVariable String name) {
return "ID:" + id + ", name:" + name;
}
http: // localhost: 8080 / api / staff / 1 / bar
----
ID: 1, name: bar
We can also manage more than one parameter @PathVariable using the java.util.Map
method parameter < String, String >:
@GetMapping ("/ api / workerswithmapvariable / {id} / {name}")
@ResponseBody
Public threads getEmployeesByIdAndNameWithMapVariable (@PathVariable Map < String, String > pathVarsMap)
String id = pathVarsMap.get ("id");
String name = pathVarsMap.get ("name");
if (id! = null && name! = null) {
return "ID:" + id + ", name:" + name;
} more {
replace "Lost Parameters";
}
}
http: // localhost: 8080 / api / staff / 1 / bar
----
ID: 1, name: bar
There is, however, a small catch while hosting many @PathVariable parameters where the path variable string contains a dot (.) Character. We've talked a lot about the corner cases here.
Using Java.util.Optional
Since the launch of Spring 4.1, we can also use java.util.Optional < T > (available in Java 8+) to manage non-compulsory flexibility:
@GetMapping (value = {"/ api / workerswithoptional", "/ api / workerswithoptional / {id}"})
@ResponseBody
Public threads getEmployeesByIdWithOptional (@PathVariable Optional < String > id) {
if (id.isPresent ()) {
return "ID:" + id.get ();
} more {
replace "Missing ID";
}
}
Now if we do not specify the default id id in the request, we get the default response:
http: // localhost: 8080 / api / staffwithoptional
----
ID is missing