Usually, when we want to request specific information from a RESTful Web Service, we usually use requests like “/student/2” or “/product/5”, where 2 and 5 are the values that can be changed depending on demand. To construct such requests in Spring MVC, we need to define the variables in the URI request and use the @PathVariable annotation to bind the method parameters to these variables. How is it in detail? Let’s find out in this tutorial.
First, I will create a Maven project as an example:
If you do not know how to create, let’s follow the instructions in this tutorial.
For the default dependencies, I have changed the version as follows:
1 2 3 4 5 6 7 8 9 10 |
<properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> <org.springframework-version>6.1.10</org.springframework-version> <org.aspectj-version>1.9.22.1</org.aspectj-version> <org.slf4j-version>2.0.13</org.slf4j-version> <org.junit-version>5.10.2</org.junit-version> <jakarta.servlet-api-version>6.1.0</jakarta.servlet-api-version> </properties> |
To run this project, I will use the Maven Jetty Plugin:
1 2 3 4 5 |
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>11.0.21</version> </plugin> |
Now, I will create a new Controller named StudentController with the following content:
1 2 3 4 5 6 7 8 |
package com.huongdanjava.springmvc; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentController { } |
Now I need to add a request for users to be able to get student information by ID in the form “student/<student_id>”, I will add as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springmvc; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentController { @GetMapping(value = "/student/{id}") String findById(@PathVariable String id) { return "Student ID: " + id; } } |
As you can see, in the request URL, I declared {id} to define an <id> variable, and with the use of the @PathVariable annotation in the method parameter, the variable in the request URL would be bound with this parameter. If you now run the application and request to the URL “http://localhost:8080/student/1” then the result will be:
Notice that the name of the variable in the request URL must be the same as the name of the parameter declared with the @PathVariable annotation. In case, the name of the method’s parameter does not match the variable name of the request URL, then you need to declare the value of the value attribute for the annotation @PathVariable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springmvc; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentController { @GetMapping(value = "/student/{id}") String findById(@PathVariable(value = "id") String studentId) { return "Student ID: " + studentId; } } |
Then, if you refresh the URL, the result is still the same.