JPA Page 通过RestTemplate传递

Can not construct instance of org.springframework.data.domain.Page

 直接上解决方案:

构造实现类(因为PageImpl没有构造函数,所以自带的PageImpl无法使用)

 
 
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class RestPageImpl<T> implements Page<T> {
    private PageImpl<T> pageDelegate = new PageImpl<>(new ArrayList<>(0));
 
    @Override
    public List<T> getContent() {
        return pageDelegate.getContent();
    }
 
    @Override
    public int getNumber() {
        return pageDelegate.getNumber();
    }
 
    @Override
    public int getNumberOfElements() {
        return pageDelegate.getNumberOfElements();
    }
 
    @Override
    public int getSize() {
        return pageDelegate.getSize();
    }
 
    @Override
    public Sort getSort() {
        return pageDelegate.getSort();
    }
 
    @Override
    public long getTotalElements() {
        return pageDelegate.getTotalElements();
    }
 
    @Override
    public int getTotalPages() {
        return pageDelegate.getTotalPages();
    }
 
    @Override
    public boolean hasContent() {
        return pageDelegate.hasContent();
    }
 
    @Override
    public boolean hasNext() {
        return pageDelegate.hasNext();
    }
 
    @Override
    public boolean hasPrevious() {
        return pageDelegate.hasPrevious();
    }
 
    @Override
    public boolean isFirst() {
        return pageDelegate.isFirst();
    }
 
    @Override
    public boolean isLast() {
        return pageDelegate.isLast();
    }
 
    @Override
    public Iterator<T> iterator() {
        return pageDelegate.iterator();
    }
 
    @Override
    public <S> Page<S> map(Converter<? super T, ? extends S> converter) {
        return pageDelegate.map(converter);
    }
 
    @Override
    public Pageable nextPageable() {
        return pageDelegate.nextPageable();
    }
 
    @Override
    public Pageable previousPageable() {
        return pageDelegate.previousPageable();
    }
 
    public void setContent(List<T> content) {
        pageDelegate = new PageImpl<>(content, null, getTotalElements());
    }
 
 
    public void setTotalElements(int totalElements) {
        pageDelegate = new PageImpl<>(getContent(), null, totalElements);
    }
 
    @Override
    public String toString() {
        return pageDelegate.toString();
    }
}
 

 

调用:

//被调用方,返回的是:ApiResult<Page<XXX>>
//接收时,使用我们的RestPageImpl
@RequestMapping(value="findPaged",method = RequestMethod.POST)
    public ApiResult<RestPageImpl<XXX>> findPaged(@RequestBody ApiRequest<XXX> requestVO)
    {
        String json= JsonConvert.toJSON(requestVO);
        HttpEntity<String> request = new HttpEntity<>(json);
        ApiResult<RestPageImpl<XXX>> result=restTemplate.exchange(
                webAPIKOBUrl+"api/findPaged"
                , HttpMethod.POST
                ,request
                ,new ParameterizedTypeReference<ApiResult<RestPageImpl<XXX>>>(){})
      .getBody();
return result; }

 

posted @ 2023-03-23 13:48  正怒月神  阅读(24)  评论(0编辑  收藏  举报