Spring Boot Path Variable Annotation | Rest Controller | Rest API

preview_player
Показать описание
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.
Рекомендации по теме
Комментарии
Автор

Hello Sir, your videos are very detailed, concise and insightful. You're also a good teacher, please do more and please share the project you talked about here on YouTube. Thank you for making this video.

mustapharaimilawal
Автор

Can I map the @Controller class (so above the class declaration) to a URI path variable?

Something like:
@Controller

public class Example {

}

henriquedelben