RestTemplate is the Spring REST Client which is used to consume RESTful Web Service APIs.
RestTemplate supports HEAD, GET, POST, PUT, DELETE, and OPTIONS Http methods.
As part of this discussion, we will be consuming the RESTful WebServices APIs of the application already developed in Building RESTful Webservices in Spring Framework using Spring Boot with Spring JDBC
Fetching All Student Records
ResponseEntity<Student[]> studentsReponse = restTemplate.getForEntity(″http://localhost:8080/students″,Student[].class);
List<Student> students = Arrays.asList(studentsReponse.getBody());
students.forEach(x -> System.out.println(x));
Output
Student [id=1, name=Gyanendra, age=15, weight=50]
Student [id=2, name=Rochit, age=16, weight=55]
Student [id=3, name=Virat, age=18, weight=47]
Student [id=6, name=Shyam, age=14, weight=48]
Student [id=8, name=Ravi, age=17, weight=65]
Student [id=9, name=Teja, age=14, weight=58]
Student [id=11, name=Viku, age=16, weight=44]
Student [id=12, name=Ajay, age=16, weight=44]
Student [id=15, name=Ramu, age=18, weight=47]
Student [id=20, name=Sachin, age=14, weight=50]
Student [id=21, name=Ramesh, age=14, weight=50]
Finding a Student with specified id
ResponseEntity<Student> findStudentResponse = restTemplate.getForEntity(″http://localhost:8080/students/1″,Student.class);
Student student = findStudentResponse.getBody();
System.out.println(“Student with id = 1:” + student);
Output
Student with id = 1:Student [id=1, name=Gyanendra, age=15, weight=50]
Updating a Student with specified id
Here we have used both put and exchange APIs of RestTemplate.
ResponseEntity<Student> findStudentResponse = restTemplate.getForEntity(″http://localhost:8080/students/1″,Student.class);
Student student = findStudentResponse.getBody();
System.out.println(“Student with id = 1:” + student);
//Update Student with id =1 using exchange API
student.setName(“Gyannn”);
student.setAge(20);
HttpEntity<Student> requestEntity = new HttpEntity<Student>(student);
ResponseEntity<Student> updatedStudentResponse = restTemplate.exchange(″http://localhost:8080/students/1″, HttpMethod.PUT, requestEntity, Student.class);
System.out.println(“Updated Student with id = 1:” + updatedStudentResponse.getBody());
student.setName(“Gyan”);
student.setAge(18);
//Update Student with id =1 using put API – put does not return anything
restTemplate.put(″http://localhost:8080/students/1″, student);
//Fetching student with id =1 again to see the changes
ResponseEntity<Student> updatedStudentR= restTemplate.getForEntity(″http://localhost:8080/students/1″,Student.class);
System.out.println(“Fetched Student with id = 1:” + updatedStudentR.getBody());
Output
Student with id = 1:Student [id=1, name=Gyanendra, age=15, weight=50]
Updated Student with id = 1:Student [id=1, name=Gyannn, age=20, weight=50]
Fetched Student with id = 1:Student [id=1, name=Gyan, age=18, weight=50]
Create a new Student record
Student newStudent = new Student(23, “Prabhakar”, 14, 50);
ResponseEntity<Student> newStudentReponse = restTemplate.postForEntity(″http://localhost:8080/addStudent″, newStudent, Student.class);
Student createdStudent = newStudentReponse.getBody();
System.out.println(“CreatedStudent:” + createdStudent);
Output
CreatedStudent:Student [id=23, name=Prabhakar, age=14, weight=50]
Delete a Student with specified id
restTemplate.delete(″http://localhost:8080/students/6″);
Now Fetching Students Again after all the Above Options
ResponseEntity<Student[]> studentsReponseAgain = restTemplate.getForEntity(″http://localhost:8080/students″,Student[].class);
List<Student> studentsAgain = Arrays.asList(studentsReponseAgain.getBody());
studentsAgain.forEach(x -> System.out.println(x));
Output
Student [id=1, name=Gyan, age=18, weight=50]
Student [id=2, name=Rochit, age=16, weight=55]
Student [id=3, name=Virat, age=18, weight=47]
Student [id=8, name=Ravi, age=17, weight=65]
Student [id=9, name=Teja, age=14, weight=58]
Student [id=11, name=Viku, age=16, weight=44]
Student [id=12, name=Ajay, age=16, weight=44]
Student [id=15, name=Ramu, age=18, weight=47]
Student [id=20, name=Sachin, age=14, weight=50]
Student [id=21, name=Ramesh, age=14, weight=50]
Student [id=23, name=Prabhakar, age=14, weight=50]
Let’s look into all the components created for building the Spring REST Client.
Project Structure
pom.xml
AppConfig
Student
MainExecutor
Output