Add support for PageSerializationMode.VIA_DTO (PagedModel) in PageJacksonModule (#1193)
Signed-off-by: Bruce Stewart <bandi.stewart@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
* Copyright 2013-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.web.PagedModel;
|
||||
|
||||
/**
|
||||
* This Jackson module provides support to deserialize Spring {@link Page} objects.
|
||||
@@ -41,6 +42,7 @@ import org.springframework.data.domain.Sort;
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Pedro Mendes
|
||||
* @author Nikita Konev
|
||||
* @author Bruce Stewart
|
||||
*/
|
||||
public class PageJacksonModule extends Module {
|
||||
|
||||
@@ -88,6 +90,7 @@ public class PageJacksonModule extends Module {
|
||||
private final Page<T> delegate;
|
||||
|
||||
SimplePageImpl(@JsonProperty("content") List<T> content, @JsonProperty("pageable") Pageable pageable,
|
||||
@JsonProperty("page") PagedModel.PageMetadata pageMetadata,
|
||||
@JsonProperty("number") @JsonAlias("pageNumber") int number,
|
||||
@JsonProperty("size") @JsonAlias("pageSize") int size,
|
||||
@JsonProperty("totalElements") @JsonAlias({ "total-elements", "total_elements", "totalelements",
|
||||
@@ -100,6 +103,11 @@ public class PageJacksonModule extends Module {
|
||||
else if (pageable != null && pageable.getPageSize() > 0) {
|
||||
delegate = new PageImpl<>(content, pageable, totalElements);
|
||||
}
|
||||
else if (pageMetadata != null && pageMetadata.size() > 0) {
|
||||
PageRequest pageRequest = buildPageRequest((int) pageMetadata.number(), (int) pageMetadata.size(),
|
||||
null);
|
||||
delegate = new PageImpl<>(content, pageRequest, pageMetadata.totalElements());
|
||||
}
|
||||
else {
|
||||
delegate = new PageImpl<>(content);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
* Copyright 2013-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -42,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Pedro Mendes
|
||||
* @author Nikita Konev
|
||||
* @author Bruce Stewart
|
||||
*/
|
||||
class PageJacksonModuleTests {
|
||||
|
||||
@@ -85,6 +86,19 @@ class PageJacksonModuleTests {
|
||||
.isEqualTo(Sort.Direction.DESC);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "./src/test/resources/withPage.json" })
|
||||
void deserializePageFromFileWithPage(String filePath) throws IOException {
|
||||
File file = new File(filePath);
|
||||
|
||||
Page<?> result = objectMapper.readValue(file, Page.class);
|
||||
|
||||
assertThat(result.getTotalElements()).isEqualTo(11);
|
||||
assertThat(result.getContent()).hasSize(10);
|
||||
assertThat(result.getPageable().getPageNumber()).isEqualTo(0);
|
||||
assertThat(result.getPageable().getSort()).isEqualTo(Sort.unsorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializeAndDeserializeEmpty() throws JsonProcessingException {
|
||||
// Given
|
||||
|
||||
70
spring-cloud-openfeign-core/src/test/resources/withPage.json
Normal file
70
spring-cloud-openfeign-core/src/test/resources/withPage.json
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"id": 3,
|
||||
"lastName": "Williams",
|
||||
"firstName": "Thomas",
|
||||
"email": "w.t@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"lastName": "Smith",
|
||||
"firstName": "James",
|
||||
"email": "s.j@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"lastName": "Scott",
|
||||
"firstName": "Steven",
|
||||
"email": "s.s@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"lastName": "Rodriguez",
|
||||
"firstName": "Daniel",
|
||||
"email": "r.d@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"lastName": "Martinez",
|
||||
"firstName": "Robert",
|
||||
"email": "m.r@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"lastName": "Jones",
|
||||
"firstName": "James",
|
||||
"email": "j.j@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"lastName": "Johnson",
|
||||
"firstName": "Robert",
|
||||
"email": "j.r@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"lastName": "Garcia",
|
||||
"firstName": "William",
|
||||
"email": "g.w@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"lastName": "Davis",
|
||||
"firstName": "Richard",
|
||||
"email": "d.r@my.domain.com"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"lastName": "Brown",
|
||||
"firstName": "Paul",
|
||||
"email": "b.p@my.domain.com"
|
||||
}
|
||||
],
|
||||
"page": {
|
||||
"number": 0,
|
||||
"size": 2,
|
||||
"totalElements": 11,
|
||||
"totalPages": 6
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user