-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for simplified rendering of Page instances via PagedModel.
This commits all necessary infrastructure to produce simplified JSON representation rendering for Page instances to make sure the representations stay stable and do not expose unnecessary implementation details. The support consists of the following elements: - PagedModel, a stripped down variant of the Spring HATEOAS counterpart to produce an equivalent JSON representation but without the hypermedia elements. This allows a gradual migration to Spring HATEOAS if needed. Page instances can be wrapped into PagedModel once and returned from controller methods to create the new, simplified JSON representation. - @EnableSpringDataWeb support now contains a pageSerializationMode attribute set to an enum with two possible values: DIRECT, which is the default for backwards compatibility reasons. It serializes Page instances directly but issues a warning that either the newly introduced support here or the Spring HATEOAS support should be used to avoid accidentally breaking representations. The other value, VIA_DTO causes all PageImpl instances to be rendered being wrapped in a PagedModel automatically by registering a Jackson StdConverter applying the wrapping transparently. Internally, the configuration of @EnableSpringDataWebSupport is translated into a bean definition of a newly introduced type SpringDataWebSettings and wired into the web configuration for consideration within a Jackson module, customizing the serialization for PageImpl. Fixes GH-3024.
- Loading branch information
Showing
7 changed files
with
359 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/org/springframework/data/web/PagedModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.web; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
/** | ||
* DTO to build stable JSON representations of a Spring Data {@link Page}. It can either be selectively used in | ||
* controller methods by calling {@code new PagedModel<>(page)} or generally activated as representation model for | ||
* {@link org.springframework.data.domain.PageImpl} instances by setting | ||
* {@link org.springframework.data.web.config.EnableSpringDataWebSupport}'s {@code pageSerializationMode} to | ||
* {@link org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode#VIA_DTO}. | ||
* | ||
* @author Oliver Drotbohm | ||
* @author Greg Turnquist | ||
* @since 3.3 | ||
*/ | ||
public class PagedModel<T> { | ||
|
||
private final Page<T> page; | ||
|
||
/** | ||
* Creates a new {@link PagedModel} for the given {@link Page}. | ||
* | ||
* @param page must not be {@literal null}. | ||
*/ | ||
public PagedModel(Page<T> page) { | ||
|
||
Assert.notNull(page, "Page must not be null"); | ||
|
||
this.page = page; | ||
} | ||
|
||
@JsonProperty | ||
public List<T> getContent() { | ||
return page.getContent(); | ||
} | ||
|
||
@Nullable | ||
@JsonProperty("page") | ||
public PageMetadata getMetadata() { | ||
return new PageMetadata(page.getSize(), page.getNumber(), page.getTotalElements(), | ||
page.getTotalPages()); | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable Object obj) { | ||
|
||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (!(obj instanceof PagedModel<?> that)) { | ||
return false; | ||
} | ||
|
||
return Objects.equals(this.page, that.page); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(page); | ||
} | ||
|
||
public static record PageMetadata(long size, long number, long totalElements, long totalPages) { | ||
|
||
public PageMetadata { | ||
Assert.isTrue(size > -1, "Size must not be negative!"); | ||
Assert.isTrue(number > -1, "Number must not be negative!"); | ||
Assert.isTrue(totalElements > -1, "Total elements must not be negative!"); | ||
Assert.isTrue(totalPages > -1, "Total pages must not be negative!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.