Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - @PathVariable Annotation


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 List studentDetails() {

				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 List numberOfStudents() {

				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.

Spring Boot - @PathVariable Annotation

Similarly,

Spring Boot - @PathVariable Annotation

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.


The @PathVariable annotation


To make use of @PathVariable annotation, the first thing you need to do is,

  1. Place the age(i.e. 8) at the end of http request ,



    http://localhost:8080/students/8

  2. The second thing you need to do is, change the code in your Controller class (i.e. 'HelloWorldController'),


    Spring Boot - @PathVariable Annotation


    @RestController
    public class HelloWorldController {
    	@Autowired
    	StudentService studentService;
    	@RequestMapping("/students/{age}")
    	public List numberOfStudents(@PathVariable("age") int age) {
    		return studentService.studentDetailsByAge(age);
    	}
    }


    Now if you look the the @RequestMapping annotation,

    @RequestMapping("/students/{age}")


    You can see that mapping is done with /students/{age}.

    And what actually happens is the age (i.e. 8) is taken from request and is added to the {age} of @RequestMapping.
    Spring Boot - @PathVariable Annotation


    Now, as seen in the above image, the age acts as a variable and 8 is placed in age variable.
    Spring Boot - @PathVariable Annotation


    This age (i.e. 8) is also available in the age of @PathVariable.

    Then in the next line (i.e. In the method numberOfStudents(...)),

    public List numberOfStudents(@PathVariable("age") int age)


    The @PathVariable("age") has 8 in it. The age (i.e. 8) from age variable is taken and is placed in int age.
    Spring Boot - @PathVariable Annotation

  3. Next in the return statement, you ask the class teacher of class 5 to give you the List

    of students of age 8.

    And how do you do that?

    You call the Service studentDetailsByAge(age) in the return statement.

    return studentService.studentDetailsByAge(age);

  4. And the class teacher creates a method named studentDetailsByAge(int age) in the Service

    class studentService.
    Spring Boot - @PathVariable Annotation


    @Service
    public class StudentService {
    	public List studentDetails() {
    		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;
    }


    That loads all the students using the studentDetails() method.

    List studentList = studentDetails();


    And loops through all the students, and takes those students whose age is 8.

    for (Student student : studentList) {
    	if (student.getAge() == age) {
    		studentsByAge.add(student);
    	}
    }


    And adds to the List studentsByAge.

    if (student.getAge() == age) {
    	studentsByAge.add(student);
    }


    And finally returns the list, studentsByAge that contains all the students whose age is 8.

    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
Spring Boot - @PathVariable Annotation


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
Spring Boot - @PathVariable Annotation


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}]

Note : The output that we are getting as response is in JSON format.