Before we begin understanding @PathVariable Annotation, let us have a quick recap of the previous tutorial.
Click here for a quick recap
In the previous tutorial we have seen that you were the Principal in a school. And a school inspector came to visit the school.
The school inspector asked you to get all the details of the students, who are in class 5.
For simplicity we have assumed that there are only three students in class 5.
Name | Roll | Age | ClassName |
---|---|---|---|
John | 1 | 8 | 5 |
Paul | 2 | 7 | 5 |
Andrew | 3 | 8 | 5 |
Now, you being the Principal of the school, asked the class teacher of class 5 to get the details of the students of class 5.
He created a Service class annotating with @Service annotation, and given you the details of the students of class 5.
@Service public class StudentService { public ListstudentDetails() { List studentList = new ArrayList<>(); Student student1 = new Student("John", 1, 8, 5); Student student2 = new Student("Paul", 2, 7, 5); Student student3 = new Student("Andrew", 3, 8, 5); studentList.add(student1); studentList.add(student2); studentList.add(student3); return studentList; } }
And, in the Controller, you (The Principal) calls studentDetails() method of the Service StudentService and returns the details of the Students.
@RestController public class HelloWorldController { @Autowired StudentService studentService; @RequestMapping("/students") public ListnumberOfStudents() { return studentService.studentDetails(); } }
And, the school inspector got the list of students of class 5.
Name | Roll | Age | ClassName |
---|---|---|---|
John | 1 | 8 | 5 |
Paul | 2 | 7 | 5 |
Andrew | 3 | 8 | 5 |
Now, the school inspector comes up with a new query.
As he finds that the age of John and Andrew is 8, he wants all the students of class 5 whose age is 8.
Now, you being the Principal, you can ask the class teacher to get the List of of all the students of class 5 whose age is 8.
Fair enough!
But what if the school inspector tells you to give the List of all the students whose age is 7.
So, this time you need to be a little smart. And in the request itself, try to pass the age.
i.e. When the school Inspector is expecting students of age 8. The request should be,
http://localhost:8080/students/8
Similarly, when the school Inspector is expecting students of age 7. The request should be,
http://localhost:8080/students/7
All we are doing is, adding the age at the end of the request.
So, let's analyse the request carefully,
We are placing the age at the end of the request.
Similarly,
So, the age (i.e. 7 or 8) looks like a variable in the path of the request. And this is exactly where @PathVariable annotation comes as rescue.
@PathVariable simply means, a variable in the request path.
Does it sound a little critical?
Let's try making it simple with the below example with pathvariable. And we are going to use path variable with Rest Controller.
To make use of @PathVariable annotation, the first thing you need to do is,
http://localhost:8080/students/8
@RestController public class HelloWorldController { @Autowired StudentService studentService; @RequestMapping("/students/{age}") public ListnumberOfStudents(@PathVariable("age") int age) { return studentService.studentDetailsByAge(age); } }
@RequestMapping("/students/{age}")
public ListnumberOfStudents(@PathVariable("age") int age)
return studentService.studentDetailsByAge(age);
@Service public class StudentService { public ListstudentDetails() { List studentList = new ArrayList<>(); Student student1 = new Student("John", 1, 8, 5); Student student2 = new Student("Paul", 2, 7, 5); Student student3 = new Student("Andrew", 3, 8, 5); studentList.add(student1); studentList.add(student2); studentList.add(student3); return studentList; } public List studentDetailsByAge(int age) { List studentsByAge = new ArrayList<>(); List studentList = studentDetails(); for (Student student : studentList) { if (student.getAge() == age) { studentsByAge.add(student); } } return studentsByAge; } } And the class teacher has created the method, public List studentDetailsByAge(int age) { List studentsByAge = new ArrayList<>(); List studentList = studentDetails(); for (Student student : studentList) { if (student.getAge() == age) { studentsByAge.add(student); } } return studentsByAge; }
ListstudentList = studentDetails();
for (Student student : studentList) { if (student.getAge() == age) { studentsByAge.add(student); } }
if (student.getAge() == age) { studentsByAge.add(student); }
return studentsByAge;
Now let us open a browser and hit the http request to get the details of the students of age 8.
http://localhost:8080/students/8
And as we can see, we got the list of all the students whose age is 8.
[{"name":"John","roll":1,"age":8,"className":5},{"name":"Andrew","roll":3,"age":8,"className":5}]
Similarly, if you want to check the details of all the students of age 7,
http://localhost:8080/students/7
And as we can see, we got the list of all the students whose age is 7,
[{"name":"Paul","roll":2,"age":7,"className":5}]