Spring RestTemplate.getForObject()
April 20, 2020
This page will walk through Spring RestTemplate.getForObject()
method example. The getForObject
method fetches the data for the given response type from the given URI or URL template using HTTP GET method. To fetch data for the given key properties from URL template we can pass Object Varargs and Map
to getForObject
method. The getForObject
returns directly the object of given response type. The getForObject
method is useful when response header information is not needed.
Here on this page we will discuss using
getForObject
method in our REST client application.
Contents
1. Technologies Used
Find the technologies being used in our example.1. Java 11
2. Spring 5.2.5.RELEASE
3. Spring Boot 2.2.6.RELEASE
4. Maven 3.5.2
2. Maven Dependencies
Find the Maven dependencies to run the example.<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
3. Using getForObject()
Find thegetForObject
method declaration with URI + responseType from Spring doc.
T getForObject(URI url, Class<T> responseType)
responseType: Type of object returned by
getForObject
method.
The
getForObject
returns the object of given response type.
Client code
URI uri = new URI("http://localhost:8080/employee"); RestTemplate restTemplate = new RestTemplate(); Employee[] emps = restTemplate.getForObject(uri, Employee[].class);
@GetMapping("employee") public ResponseEntity<List<Employee>> getAllEmployees() { ------ }
4. URI Variables as Object Varargs
Find thegetForObject
method declaration with url + responseType + uriVariables as Object Varargs from Spring doc.
T getForObject(String url, Class<T> responseType, Object... uriVariables)
responseType: Type of object returned by
getForObject
method.
uriVariables: URI variables as Object Varargs.
Client code
String url = "http://localhost:8080/employee/{id}"; Integer empId= 101; Employee emp = restTemplate.getForObject(url, Employee.class, empId);
@GetMapping("employee/{id}") public ResponseEntity<Employee> getEmployeeById(@PathVariable("id") Integer empId) { ------ }
5. URI Variables as Map
Find thegetForObject
method declaration with url + responseType + uriVariables as Map from Spring doc.
T getForObject(String url, Class<T> responseType, Map<String,?> uriVariables)
Map
.
Client code
String url = "http://localhost:8080/employee/{profile}/{tech}"; Map<String, String> map = new HashMap<>(); map.put("profile", "Developer"); map.put("tech", "Java"); Employee[] emps = restTemplate.getForObject(url, Employee[].class, map);
Map
. If we want to use object varargs in the above code, we can do it as following.
String profile = "Developer"; String tech = "Java"; Employee[] emps = restTemplate.getForObject(url, Employee[].class, profile, tech);
@GetMapping("employee/{profile}/{tech}") public ResponseEntity<List<Employee>> getEmployeesByProfileNTech(@PathVariable("profile") String profile, @PathVariable("tech") String technology) { ------ }
6. Complete Example
Find the client code.RestClientUtil.java
package com.concretepage.client; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; import org.springframework.web.client.RestTemplate; import com.concretepage.domain.Employee; public class RestClientUtil { public void getEmployeeDemo1() throws URISyntaxException { URI uri = new URI("http://localhost:8080/employee"); RestTemplate restTemplate = new RestTemplate(); Employee[] emps = restTemplate.getForObject(uri, Employee[].class); for (Employee e : emps) { System.out.println(e); } } public void getEmployeeDemo2() { String url = "http://localhost:8080/employee/{id}"; Integer empId= 101; RestTemplate restTemplate = new RestTemplate(); Employee emp = restTemplate.getForObject(url, Employee.class, empId); System.out.println(emp); } public void getEmployeeDemo3() { String url = "http://localhost:8080/employee/{profile}/{tech}"; Map<String, String> map = new HashMap<>(); map.put("profile", "Developer"); map.put("tech", "Java"); RestTemplate restTemplate = new RestTemplate(); Employee[] emps = restTemplate.getForObject(url, Employee[].class, map); for (Employee e : emps) { System.out.println(e); } } public static void main(String args[]) throws URISyntaxException { RestClientUtil util = new RestClientUtil(); util.getEmployeeDemo1(); util.getEmployeeDemo2(); util.getEmployeeDemo3(); } }
Find the Server code.
EmployeeController.java
package com.concretepage.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.concretepage.domain.Employee; import com.concretepage.service.EmployeeService; @RestController public class EmployeeController { @Autowired private EmployeeService empService; @GetMapping("employee") public ResponseEntity<List<Employee>> getAllEmployees() { List<Employee> list = empService.getAllEmployees(); return new ResponseEntity<List<Employee>>(list, HttpStatus.OK); } @GetMapping("employee/{id}") public ResponseEntity<Employee> getEmployeeById(@PathVariable("id") Integer empId) { Employee emp = empService.getEmployeeById(empId); return new ResponseEntity<Employee>(emp, HttpStatus.OK); } @GetMapping("employee/{profile}/{tech}") public ResponseEntity<List<Employee>> getEmployeesByProfileNTech(@PathVariable("profile") String profile, @PathVariable("tech") String technology) { List<Employee> list = empService.getEmployeesByProfileNTech(profile, technology); return new ResponseEntity<List<Employee>>(list, HttpStatus.OK); } }
package com.concretepage.domain; public class Employee { private int empId; private String name; private String city; public Employee() {} public Employee(int empId, String name, String city) { this.empId = empId; this.name = name; this.city = city; } //Setters and Getters }
package com.concretepage.service; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Service; import com.concretepage.domain.Employee; @Service public class EmployeeService { public Employee getEmployeeById(int empId) { // Perform database operation System.out.println("empId: " + empId); return new Employee(101, "Lakshman", "Agra"); } public List<Employee> getEmployeesByProfileNTech(String profile, String technology) { // Perform database operation System.out.println(profile); System.out.println(technology); Employee e1 = new Employee(101, "Lakshman", "Agra"); Employee e2 = new Employee(102, "Bharat", "Mathura"); return Arrays.asList(e1, e2); } public List<Employee> getAllEmployees() { // Perform database operation Employee e1 = new Employee(101, "Lakshman", "Agra"); Employee e2 = new Employee(102, "Bharat", "Mathura"); Employee e3 = new Employee(103, "Shatrughan", "Noida"); return Arrays.asList(e1, e2, e3); } }
getEmployeeDemo1()
method of RestClientUtil
class.
101, Lakshman, Agra 102, Bharat, Mathura 103, Shatrughan, Noida
7. Run Application
Download the project and run the following command from root folder of the project using command prompt.mvn spring-boot:run
RestClientUtil
as Java Application.