Spring PageImpl包含的内容太多了吗?我们能创建自定义的Page Impl吗?

huangapple go评论62阅读模式
英文:

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&lt;T&gt; implements Page&lt;T&gt; {
private final Page&lt;T&gt; delegate;
public CustomPageImpl(List&lt;T&gt; 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&lt;T&gt; 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 &lt;U&gt; Page&lt;U&gt; map(final Function&lt;? super T, ? extends U&gt; function) {
return this.delegate.map(function);
}
@JsonIgnore
public Iterator&lt;T&gt; iterator() {
return this.delegate.iterator();
}

}

huangapple
  • 本文由 发表于 2023年2月16日 19:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定