Hi guys,
In this lesson, we will talk about spring path variables.
Spring provides a PathVariable annotation for rest api.
The @PathVariable annotation is used to handle template variables in the request URI mapping, and use them as method parameters.
Let’s check it for an example.
For example, if we use @PathVariable annotation to extract the templated part of the URI represented by the variable {userId},
We can send a request like /api/example/{userId}.
This request will invoke our example method with the extracted user-id value like 1234.
In spring, we can also specify the path variable name. In default, the path variable uses the same name with the parameter name. But we can customize it with the PathVariable name property.
In Spring, method parameters annotated with @PathVariable are required by default. But, PathVariable can be optional also.
We can set the required property of @PathVariable to false to make it optional.
We will see the details of it on our project.
That’s all for the beginning.
Thank you.
Watch video Spring Boot Path Variable Annotation | Rest Controller | Rest API online without registration, duration hours minute second in high quality. This video was added by user Senol Atac 19 February 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 338 once and liked it 4 people.