Spring Data Pagination returns no results with JSONView

后端 未结 6 1536
广开言路
广开言路 2021-02-06 02:53

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

6条回答
  •  粉色の甜心
    2021-02-06 03:31

    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();
        }
    
    }
    

提交回复
热议问题