Updated April 10, 2023
Definition of Spring Boot Path Variable
In Spring boot application, we have path variable which sent in the URL. In spring boot it is an annotation that tells us that this parameter will be sent in the URI only, we have to follow the proper syntax and standard defined by the spring boot framework. We should also have the required dependency in place, in order to use this in our project of spring boot. While sending this data to spring boot application the parameter should also be correct otherwise it will throw some error. In the coming section of the tutorial, we will see the step-by-step guide to using this in our application, also the required configuration to use this in the program for beginners.
Syntax:
In this section, we will see the syntax for the path variable in spring boot, as we already know that they used to map the parameter from the URI in spring. Let’s take a closer look at the syntax for the path variable for better understanding see below;
@RequestMapping(path="/{your path}/{your path}")
public String getMessage(@PathVariable("variable_name") String variable,
@PathVariable("variable_name") String variable) {
// logic will go here
}
As you can see in the above syntax for path variable we have used the @PathVariable annotation here to represent the variable as the path variable. Also, let’s take a sample piece of code to understand the usage of path variable in detail see below;
e.g. :
@RequestMapping(path="/{id}")
public String demo(@PathVariable("id") int id) {
// logic will go here
}
As from the above piece of code it is simple to understand and implement this. In the coming section of the tutorial, we will see how we can use this while programming for beginners.
How path variable works in Spring boot?
As of now we already know that path variable is used to specify the variable in the request URI, also we have to follow the standard and syntax defined by the spring boot framework in order to get the value at controller, otherwise, it will throw us runtime exception saying 417 or 404. In order to use the path variable, we will first see the syntax and then sample a piece of code to test and run it in our application, we can test or URL from the postman or any tool, or from UI also if you have one.
1) Use: this is a simple annotation in the spring boot framework, which can be used with the method parameter in order to get the value from the URL. We may have some cases where it is not always feasible to send the object to the controller sometime we may require to get the data based on some specific params only, then we can go for the path variable. Also, it does not require any configuration to use this they are just like any other annotation in the spring boot framework.
Syntax:
@PathVariable
2) How to use this: in order to use this, we need to have the following dependency in place, for the spring boot application. We should have the web dependency for spring boot in place because this parameter usually related to the URI only. Below you can find the package name where we can find it see below;
e.g. :
org.springframework.web.bind.annotation;
We have to have this package in place in order to use this. After this, we can directly use this annotation in our project.
3) Over method: We can use this annotation with the method parameter, the required parameter should be annotated using this annotation in spring boot. We will see one syntax of how we use this while programming in spring boot;
e.g:
@RequestMapping(path="/{rollno}")
public String demo(@PathVariable("rollno") int rollno) {
// logic will go here
}
As you can see in the above piece of code we have specified some of the value with it lets discuss each of them in detail see below;
a) required: This is the most common and frequently used option with the path variable in spring boot. It tells that this parameter is required in the URL, if not present it will throw an error for us. IT can act as a validation also for us if we want to validate the value before use.
b) name: This is also an optional value for the path variable in spring boot by the use of it we can tell which parameter we need to bind its value too.
c) value: By the use of it, we can specify an alias for the parameter we have created to access it by that name.
4) how to call the URL: After making all the changes to the code, now we have to call the URL and pass some parameters inside it. so for that wit should be separated by the forward-slash in the URL and the value for the parameters. Remember it should be in the same order that you have created in the controller, which we will see in detail in the example section.
Steps;
1) Make a simple project from the spring initializer and try to do the following changes inside it.
2)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(DemoApplication.class, args);
}
}
3) After that make one controller and make on the method which takes path variable as the parameter
4)
@RestController
@CrossOrigin
@RequestMapping("/employee/")
public class EmployeeController {
@GetMapping("get/{name}/{rollno}")
public String getData(@PathVariable String name, @PathVariable String rollno) {
return // our logic ..//
}
}
5) and inside the service class, we can perform the required logic to our code to get the desired output. from the db or any other logic.
Conclusion
By the use of the path variable in spring boot, we can bind our variable to the request URL. This is very easy to sue and handle that we have already seen in the above example and piece of code. But to fully test and configure t we have to make an application from scratch to test it with all necessary configuration inside it.
Recommended Articles
This is a guide to Spring Boot Path Variable. Here we discuss definition, syntax, and parameters, How path variable works in Spring boot? examples with code implementation. You may also have a look at the following articles to learn more –