BAEL-1763: Get and post lists with RestTemplate (#4438)
* Code showing how to use Lists with RestTemplate * Update URLs to work inside spring-rest project * Refactor Employee xtor; rename EmployeeListDTO
This commit is contained in:
parent
2cb8d357f4
commit
efa7186530
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.resttemplate.lists;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Sample application used to demonstrate working with Lists and RestTemplate.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class EmployeeApplication
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
SpringApplication.run(EmployeeApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package org.baeldung.resttemplate.lists.client;
|
||||
|
||||
import org.baeldung.resttemplate.lists.dto.Employee;
|
||||
import org.baeldung.resttemplate.lists.dto.EmployeeList;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Application that shows how to use Lists with RestTemplate.
|
||||
*/
|
||||
public class EmployeeClient
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
EmployeeClient employeeClient = new EmployeeClient();
|
||||
|
||||
System.out.println("Calling GET using ParameterizedTypeReference");
|
||||
employeeClient.getAllEmployeesUsingParameterizedTypeReference();
|
||||
|
||||
System.out.println("Calling GET using wrapper class");
|
||||
employeeClient.getAllEmployeesUsingWrapperClass();
|
||||
|
||||
System.out.println("Calling POST using normal lists");
|
||||
employeeClient.createEmployeesUsingLists();
|
||||
|
||||
System.out.println("Calling POST using wrapper class");
|
||||
employeeClient.createEmployeesUsingWrapperClass();
|
||||
}
|
||||
|
||||
public EmployeeClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesUsingParameterizedTypeReference()
|
||||
{
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
ResponseEntity<List<Employee>> response =
|
||||
restTemplate.exchange(
|
||||
"http://localhost:8080/spring-rest/employees/",
|
||||
HttpMethod.GET,
|
||||
null,
|
||||
new ParameterizedTypeReference<List<Employee>>(){});
|
||||
|
||||
List<Employee> employees = response.getBody();
|
||||
|
||||
employees.forEach(e -> System.out.println(e));
|
||||
|
||||
return employees;
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesUsingWrapperClass()
|
||||
{
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
EmployeeList response =
|
||||
restTemplate.getForObject(
|
||||
"http://localhost:8080/spring-rest/employees/v2",
|
||||
EmployeeList.class);
|
||||
|
||||
List<Employee> employees = response.getEmployees();
|
||||
|
||||
employees.forEach(e -> System.out.println(e));
|
||||
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void createEmployeesUsingLists()
|
||||
{
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
List<Employee> newEmployees = new ArrayList<>();
|
||||
newEmployees.add(new Employee(3, "Intern"));
|
||||
newEmployees.add(new Employee(4, "CEO"));
|
||||
|
||||
restTemplate.postForObject(
|
||||
"http://localhost:8080/spring-rest/employees/",
|
||||
newEmployees,
|
||||
ResponseEntity.class);
|
||||
}
|
||||
|
||||
public void createEmployeesUsingWrapperClass()
|
||||
{
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
List<Employee> newEmployees = new ArrayList<>();
|
||||
newEmployees.add(new Employee(3, "Intern"));
|
||||
newEmployees.add(new Employee(4, "CEO"));
|
||||
|
||||
restTemplate.postForObject(
|
||||
"http://localhost:8080/spring-rest/employees/v2/",
|
||||
new EmployeeList(newEmployees),
|
||||
ResponseEntity.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package org.baeldung.resttemplate.lists.controller;
|
||||
|
||||
import org.baeldung.resttemplate.lists.dto.Employee;
|
||||
import org.baeldung.resttemplate.lists.dto.EmployeeList;
|
||||
import org.baeldung.resttemplate.lists.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/employees")
|
||||
public class EmployeeResource
|
||||
{
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, path = "/")
|
||||
public List<Employee> getEmployees()
|
||||
{
|
||||
return employeeService.getAllEmployees();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, path = "/v2")
|
||||
public EmployeeList getEmployeesUsingWrapperClass()
|
||||
{
|
||||
List<Employee> employees = employeeService.getAllEmployees();
|
||||
return new EmployeeList(employees);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, path = "/")
|
||||
public void addEmployees(@RequestBody List<Employee> employees)
|
||||
{
|
||||
employeeService.addEmployees(employees);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, path = "/v2")
|
||||
public void addEmployeesUsingWrapperClass(@RequestBody EmployeeList employeeWrapper)
|
||||
{
|
||||
employeeService.addEmployees(employeeWrapper.getEmployees());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package org.baeldung.resttemplate.lists.dto;
|
||||
|
||||
public class Employee {
|
||||
|
||||
public long id;
|
||||
public String title;
|
||||
|
||||
public Employee()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Employee(long id, String title)
|
||||
{
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Employee #" + id + "[" + title + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.resttemplate.lists.dto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EmployeeList
|
||||
{
|
||||
public List<Employee> employees;
|
||||
|
||||
public EmployeeList()
|
||||
{
|
||||
employees = new ArrayList<>();
|
||||
}
|
||||
|
||||
public EmployeeList(List<Employee> employees)
|
||||
{
|
||||
this.employees = employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<Employee> employees)
|
||||
{
|
||||
this.employees = employees;
|
||||
}
|
||||
|
||||
public List<Employee> getEmployees()
|
||||
{
|
||||
return employees;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.baeldung.resttemplate.lists.service;
|
||||
|
||||
import org.baeldung.resttemplate.lists.dto.Employee;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EmployeeService
|
||||
{
|
||||
public List<Employee> getAllEmployees()
|
||||
{
|
||||
List<Employee> employees = new ArrayList<>();
|
||||
employees.add(new Employee(1, "Manager"));
|
||||
employees.add(new Employee(2, "Java Developer"));
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void addEmployees(List<Employee> employees)
|
||||
{
|
||||
employees.forEach(e -> System.out.println("Adding new employee " + e));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue