I am using Spring data pagination in my REST Controller and returning Paged entity. I would like to control the data returned as JSON with the help of JSONViews.
I am a
I actually found a simpler and better way of doing this. The problem I had was with the fact that I cannot set @JsonView annotations on the Page object I was receiving. So I created an implementation of the Page interface and added my JsonViews to it. And instead of returning Page, I am now returning MyPage
public class MyPage implements Page {
private Page pageObj;
public MyPage(Page pageObj) {
this.pageObj = pageObj;
}
@JsonView(PaginatedResult.class)
@Override
public int getNumber() {
return pageObj.getNumber();
}
@Override
public int getSize() {
return pageObj.getSize();
}
@JsonView(PaginatedResult.class)
@Override
public int getNumberOfElements() {
return pageObj.getNumberOfElements();
}
@JsonView(PaginatedResult.class)
@Override
public List getContent() {
return pageObj.getContent();
}
@Override
public boolean hasContent() {
return pageObj.hasContent();
}
@Override
public Sort getSort() {
return pageObj.getSort();
}
@JsonView(PaginatedResult.class)
@Override
public boolean isFirst() {
return pageObj.isFirst();
}
@JsonView(PaginatedResult.class)
@Override
public boolean isLast() {
return pageObj.isLast();
}
@Override
public boolean hasNext() {
return pageObj.hasNext();
}
@Override
public boolean hasPrevious() {
return pageObj.hasPrevious();
}
@Override
public Pageable nextPageable() {
return pageObj.nextPageable();
}
@Override
public Pageable previousPageable() {
return pageObj.previousPageable();
}
@Override
public Iterator iterator() {
return pageObj.iterator();
}
@JsonView(PaginatedResult.class)
@Override
public int getTotalPages() {
return pageObj.getTotalPages();
}
@JsonView(PaginatedResult.class)
@Override
public long getTotalElements() {
return pageObj.getTotalElements();
}
}