|
| 1 | +package org.baeldung.resttemplate.lists.client; |
| 2 | + |
| 3 | +import org.baeldung.resttemplate.lists.dto.Employee; |
| 4 | +import org.baeldung.resttemplate.lists.dto.EmployeeList; |
| 5 | +import org.springframework.core.ParameterizedTypeReference; |
| 6 | +import org.springframework.http.*; |
| 7 | +import org.springframework.web.client.RestTemplate; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +/** |
| 13 | + * Application that shows how to use Lists with RestTemplate. |
| 14 | + */ |
| 15 | +public class EmployeeClient |
| 16 | +{ |
| 17 | + public static void main(String[] args) |
| 18 | + { |
| 19 | + EmployeeClient employeeClient = new EmployeeClient(); |
| 20 | + |
| 21 | + System.out.println("Calling GET using ParameterizedTypeReference"); |
| 22 | + employeeClient.getAllEmployeesUsingParameterizedTypeReference(); |
| 23 | + |
| 24 | + System.out.println("Calling GET using wrapper class"); |
| 25 | + employeeClient.getAllEmployeesUsingWrapperClass(); |
| 26 | + |
| 27 | + System.out.println("Calling POST using normal lists"); |
| 28 | + employeeClient.createEmployeesUsingLists(); |
| 29 | + |
| 30 | + System.out.println("Calling POST using wrapper class"); |
| 31 | + employeeClient.createEmployeesUsingWrapperClass(); |
| 32 | + } |
| 33 | + |
| 34 | + public EmployeeClient() |
| 35 | + { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public List<Employee> getAllEmployeesUsingParameterizedTypeReference() |
| 40 | + { |
| 41 | + RestTemplate restTemplate = new RestTemplate(); |
| 42 | + |
| 43 | + ResponseEntity<List<Employee>> response = |
| 44 | + restTemplate.exchange( |
| 45 | + "http://localhost:8080/spring-rest/employees/", |
| 46 | + HttpMethod.GET, |
| 47 | + null, |
| 48 | + new ParameterizedTypeReference<List<Employee>>(){}); |
| 49 | + |
| 50 | + List<Employee> employees = response.getBody(); |
| 51 | + |
| 52 | + employees.forEach(e -> System.out.println(e)); |
| 53 | + |
| 54 | + return employees; |
| 55 | + } |
| 56 | + |
| 57 | + public List<Employee> getAllEmployeesUsingWrapperClass() |
| 58 | + { |
| 59 | + RestTemplate restTemplate = new RestTemplate(); |
| 60 | + |
| 61 | + EmployeeList response = |
| 62 | + restTemplate.getForObject( |
| 63 | + "http://localhost:8080/spring-rest/employees/v2", |
| 64 | + EmployeeList.class); |
| 65 | + |
| 66 | + List<Employee> employees = response.getEmployees(); |
| 67 | + |
| 68 | + employees.forEach(e -> System.out.println(e)); |
| 69 | + |
| 70 | + return employees; |
| 71 | + } |
| 72 | + |
| 73 | + public void createEmployeesUsingLists() |
| 74 | + { |
| 75 | + RestTemplate restTemplate = new RestTemplate(); |
| 76 | + |
| 77 | + List<Employee> newEmployees = new ArrayList<>(); |
| 78 | + newEmployees.add(new Employee(3, "Intern")); |
| 79 | + newEmployees.add(new Employee(4, "CEO")); |
| 80 | + |
| 81 | + restTemplate.postForObject( |
| 82 | + "http://localhost:8080/spring-rest/employees/", |
| 83 | + newEmployees, |
| 84 | + ResponseEntity.class); |
| 85 | + } |
| 86 | + |
| 87 | + public void createEmployeesUsingWrapperClass() |
| 88 | + { |
| 89 | + RestTemplate restTemplate = new RestTemplate(); |
| 90 | + |
| 91 | + List<Employee> newEmployees = new ArrayList<>(); |
| 92 | + newEmployees.add(new Employee(3, "Intern")); |
| 93 | + newEmployees.add(new Employee(4, "CEO")); |
| 94 | + |
| 95 | + restTemplate.postForObject( |
| 96 | + "http://localhost:8080/spring-rest/employees/v2/", |
| 97 | + new EmployeeList(newEmployees), |
| 98 | + ResponseEntity.class); |
| 99 | + } |
| 100 | +} |
0 commit comments