英文:
Why Spring PageImpl contains too many contents ? Can we create a custom Page Impl?
问题
这是我在访问分页端点时获得的数值。
在这里,我可以看到除了所需的数据之外,还有其他多个值,如排序(sort)、已分页(paged)和未分页(unpaged)。根据我的要求,这些细节是不必要的。
是否有办法限制页面数据类型包含的数据,比如创建一个CustomPageImpl
?如果可以的话,最好的方式是什么?
我明白我可以创建一个新的DTO并根据要求传输数据,但我想了解最佳实践。
谢谢。
英文:
I am using Default Page Type provided by Spring.
This is the values I am getting when I am hitting the paginated endpoint.
{
"content": [
],
"pageable": {
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 10,
"last": true,
"size": 20,
"number": 0,
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"numberOfElements": 10,
"first": true,
"empty": false
}
Over here I can see multiple other values other than the needed data like sort, paged and unpaged. These details aren't required as per my requirement.
Is there any way I could limit the data that page datatype contains, like creating a CustomPageImpl
? If yes what would be the best way.
I get that I could create a new DTO and transfer the data based on the requirements, however I would like to know the best practices.
Thanks
答案1
得分: 1
创建一个用于实现Page接口的类,并使用@JsonIgnore来忽略不必要的字段。
public class CustomPageImpl<T> implements Page<T> {
private final Page<T> delegate;
public CustomPageImpl(List<T> content, int number, int size, long totalElements) {
this.delegate = new PageImpl(content, PageRequest.of(number, size), totalElements);
}
@JsonProperty
public int getTotalPages() {
return this.delegate.getTotalPages();
}
@JsonProperty
public long getTotalElements() {
return this.delegate.getTotalElements();
}
@JsonProperty
public int getNumber() {
return this.delegate.getNumber();
}
@JsonProperty
public int getSize() {
return this.delegate.getSize();
}
@JsonProperty
public int getNumberOfElements() {
return this.delegate.getNumberOfElements();
}
@JsonProperty
public List<T> getContent() {
return this.delegate.getContent();
}
@JsonProperty
public boolean hasContent() {
return this.delegate.hasContent();
}
@JsonIgnore
public Sort getSort() {
return this.delegate.getSort();
}
@JsonIgnore
public boolean isFirst() {
return this.delegate.isFirst();
}
@JsonIgnore
public boolean isLast() {
return this.delegate.isLast();
}
@JsonIgnore
public boolean hasNext() {
return this.delegate.hasNext();
}
@JsonIgnore
public boolean hasPrevious() {
return this.delegate.hasPrevious();
}
@JsonIgnore
public Pageable nextPageable() {
return this.delegate.nextPageable();
}
@JsonIgnore
public Pageable previousPageable() {
return this.delegate.previousPageable();
}
public <U> Page<U> map(final Function<? super T, ? extends U> function) {
return this.delegate.map(function);
}
@JsonIgnore
public Iterator<T> iterator() {
return this.delegate.iterator();
}
}
英文:
Create a class for implementing Page interface and use @JsonIgnore for ignoring unnecessary fields.
public class CustomPageImpl<T> implements Page<T> {
private final Page<T> delegate;
public CustomPageImpl(List<T> content, int number, int size, long totalElements) {
this.delegate = new PageImpl(content, PageRequest.of(number, size), totalElements);
}
@JsonProperty
public int getTotalPages() {
return this.delegate.getTotalPages();
}
@JsonProperty
public long getTotalElements() {
return this.delegate.getTotalElements();
}
@JsonProperty
public int getNumber() {
return this.delegate.getNumber();
}
@JsonProperty
public int getSize() {
return this.delegate.getSize();
}
@JsonProperty
public int getNumberOfElements() {
return this.delegate.getNumberOfElements();
}
@JsonProperty
public List<T> getContent() {
return this.delegate.getContent();
}
@JsonProperty
public boolean hasContent() {
return this.delegate.hasContent();
}
@JsonIgnore
public Sort getSort() {
return this.delegate.getSort();
}
@JsonIgnore
public boolean isFirst() {
return this.delegate.isFirst();
}
@JsonIgnore
public boolean isLast() {
return this.delegate.isLast();
}
@JsonIgnore
public boolean hasNext() {
return this.delegate.hasNext();
}
@JsonIgnore
public boolean hasPrevious() {
return this.delegate.hasPrevious();
}
@JsonIgnore
public Pageable nextPageable() {
return this.delegate.nextPageable();
}
@JsonIgnore
public Pageable previousPageable() {
return this.delegate.previousPageable();
}
public <U> Page<U> map(final Function<? super T, ? extends U> function) {
return this.delegate.map(function);
}
@JsonIgnore
public Iterator<T> iterator() {
return this.delegate.iterator();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论