[BAEL-6318] code for consuming Page<Entity> using rest template
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class CustomPageImpl<T> extends PageImpl<T> {
|
||||
|
||||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
|
||||
public CustomPageImpl(@JsonProperty("content") List<T> content, @JsonProperty("number") int number, @JsonProperty("size") int size, @JsonProperty("totalElements") Long totalElements, @JsonProperty("pageable") JsonNode pageable,
|
||||
@JsonProperty("last") boolean last, @JsonProperty("totalPages") int totalPages, @JsonProperty("sort") JsonNode sort, @JsonProperty("numberOfElements") int numberOfElements) {
|
||||
super(content, PageRequest.of(number, 1), 10);
|
||||
|
||||
}
|
||||
|
||||
public CustomPageImpl(List<T> content, Pageable pageable, long total) {
|
||||
super(content, pageable, total);
|
||||
}
|
||||
|
||||
public CustomPageImpl(List<T> content) {
|
||||
super(content);
|
||||
}
|
||||
|
||||
public CustomPageImpl() {
|
||||
super(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
@Component
|
||||
public class EmployeeClient {
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public EmployeeClient(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public Page<EmployeeDto> getEmployeeDataFromExternalAPI(Pageable pageable) {
|
||||
String url = "http://localhost:8080/external-service/employee";
|
||||
|
||||
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(url)
|
||||
.queryParam("page", pageable.getPageNumber())
|
||||
.queryParam("size", pageable.getPageSize());
|
||||
|
||||
ResponseEntity<CustomPageImpl<EmployeeDto>> responseEntity = restTemplate.exchange(uriBuilder.toUriString(), HttpMethod.GET, null, new ParameterizedTypeReference<CustomPageImpl<EmployeeDto>>() {
|
||||
});
|
||||
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/organisation")
|
||||
public class EmployeeController {
|
||||
|
||||
private final EmployeeService organisationService;
|
||||
|
||||
public EmployeeController(EmployeeService organisationService) {
|
||||
this.organisationService = organisationService;
|
||||
}
|
||||
|
||||
@GetMapping("/employee")
|
||||
public ResponseEntity<Page<EmployeeDto>> getEmployeeData(Pageable pageable) {
|
||||
Page<EmployeeDto> employeeData = organisationService.getEmployeeData(pageable);
|
||||
return ResponseEntity.ok(employeeData);
|
||||
}
|
||||
|
||||
@GetMapping("/data")
|
||||
public ResponseEntity<Page<EmployeeDto>> getData(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
|
||||
List<EmployeeDto> empList = listImplementation();
|
||||
|
||||
int totalSize = empList.size();
|
||||
int startIndex = page * size;
|
||||
int endIndex = Math.min(startIndex + size, totalSize);
|
||||
|
||||
List<EmployeeDto> pageContent = empList.subList(startIndex, endIndex);
|
||||
|
||||
Page<EmployeeDto> employeeDtos = new PageImpl<>(pageContent, PageRequest.of(page, size), totalSize);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.body(employeeDtos);
|
||||
}
|
||||
|
||||
private static List<EmployeeDto> listImplementation() {
|
||||
List<EmployeeDto> empList = new ArrayList<>();
|
||||
empList.add(new EmployeeDto("Jane", "Finance", 50000));
|
||||
empList.add(new EmployeeDto("Sarah", "IT", 70000));
|
||||
empList.add(new EmployeeDto("John", "IT", 90000));
|
||||
return empList;
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class EmployeeDto {
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
|
||||
@JsonProperty("dept")
|
||||
private String dept;
|
||||
|
||||
@JsonProperty("salary")
|
||||
private long salary;
|
||||
|
||||
public EmployeeDto() {
|
||||
}
|
||||
|
||||
public EmployeeDto(String name, String dept, long salary) {
|
||||
this.name = name;
|
||||
this.dept = dept;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDept() {
|
||||
return dept;
|
||||
}
|
||||
|
||||
public void setDept(String dept) {
|
||||
this.dept = dept;
|
||||
}
|
||||
|
||||
public long getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(long salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
}
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
private final EmployeeClient employeeClient;
|
||||
|
||||
public EmployeeService(EmployeeClient employeeClient) {
|
||||
this.employeeClient = employeeClient;
|
||||
}
|
||||
|
||||
public Page<EmployeeDto> getEmployeeData(Pageable pageable) {
|
||||
return employeeClient.getEmployeeDataFromExternalAPI(pageable);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PageEntityResponseApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PageEntityResponseApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.pageentityresponse;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user