Skip to content

Commit efa7186

Browse files
michael-prattpivovarit
authored andcommitted
BAEL-1763: Get and post lists with RestTemplate (eugenp#4438)
* Code showing how to use Lists with RestTemplate * Update URLs to work inside spring-rest project * Refactor Employee xtor; rename EmployeeListDTO
1 parent 2cb8d35 commit efa7186

6 files changed

Lines changed: 254 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.resttemplate.lists;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Sample application used to demonstrate working with Lists and RestTemplate.
8+
*/
9+
@SpringBootApplication
10+
public class EmployeeApplication
11+
{
12+
public static void main(String[] args)
13+
{
14+
SpringApplication.run(EmployeeApplication.class, args);
15+
}
16+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.baeldung.resttemplate.lists.controller;
2+
3+
import org.baeldung.resttemplate.lists.dto.Employee;
4+
import org.baeldung.resttemplate.lists.dto.EmployeeList;
5+
import org.baeldung.resttemplate.lists.service.EmployeeService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.List;
13+
14+
@RestController
15+
@RequestMapping("/employees")
16+
public class EmployeeResource
17+
{
18+
@Autowired
19+
private EmployeeService employeeService;
20+
21+
@RequestMapping(method = RequestMethod.GET, path = "/")
22+
public List<Employee> getEmployees()
23+
{
24+
return employeeService.getAllEmployees();
25+
}
26+
27+
@RequestMapping(method = RequestMethod.GET, path = "/v2")
28+
public EmployeeList getEmployeesUsingWrapperClass()
29+
{
30+
List<Employee> employees = employeeService.getAllEmployees();
31+
return new EmployeeList(employees);
32+
}
33+
34+
@RequestMapping(method = RequestMethod.POST, path = "/")
35+
public void addEmployees(@RequestBody List<Employee> employees)
36+
{
37+
employeeService.addEmployees(employees);
38+
}
39+
40+
@RequestMapping(method = RequestMethod.POST, path = "/v2")
41+
public void addEmployeesUsingWrapperClass(@RequestBody EmployeeList employeeWrapper)
42+
{
43+
employeeService.addEmployees(employeeWrapper.getEmployees());
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.baeldung.resttemplate.lists.dto;
2+
3+
public class Employee {
4+
5+
public long id;
6+
public String title;
7+
8+
public Employee()
9+
{
10+
11+
}
12+
13+
public Employee(long id, String title)
14+
{
15+
this.id = id;
16+
this.title = title;
17+
}
18+
19+
public long getId() {
20+
return id;
21+
}
22+
23+
public void setId(long id) {
24+
this.id = id;
25+
}
26+
27+
public String getTitle() {
28+
return title;
29+
}
30+
31+
public void setTitle(String title) {
32+
this.title = title;
33+
}
34+
35+
@Override
36+
public String toString()
37+
{
38+
return "Employee #" + id + "[" + title + "]";
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.resttemplate.lists.dto;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class EmployeeList
7+
{
8+
public List<Employee> employees;
9+
10+
public EmployeeList()
11+
{
12+
employees = new ArrayList<>();
13+
}
14+
15+
public EmployeeList(List<Employee> employees)
16+
{
17+
this.employees = employees;
18+
}
19+
20+
public void setEmployees(List<Employee> employees)
21+
{
22+
this.employees = employees;
23+
}
24+
25+
public List<Employee> getEmployees()
26+
{
27+
return employees;
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.resttemplate.lists.service;
2+
3+
import org.baeldung.resttemplate.lists.dto.Employee;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@Service
10+
public class EmployeeService
11+
{
12+
public List<Employee> getAllEmployees()
13+
{
14+
List<Employee> employees = new ArrayList<>();
15+
employees.add(new Employee(1, "Manager"));
16+
employees.add(new Employee(2, "Java Developer"));
17+
return employees;
18+
}
19+
20+
public void addEmployees(List<Employee> employees)
21+
{
22+
employees.forEach(e -> System.out.println("Adding new employee " + e));
23+
}
24+
}

0 commit comments

Comments
 (0)