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 implements Page {
    private PageImpl pageDelegate = new PageImpl<>(new ArrayList<>(0));

    @Override
    public List 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 iterator() {
        return pageDelegate.iterator();
    }

    @Override
    public  Page map(Converter converter) {
        return pageDelegate.map(converter);
    }

    @Override
    public Pageable nextPageable() {
        return pageDelegate.nextPageable();
    }

    @Override
    public Pageable previousPageable() {
        return pageDelegate.previousPageable();
    }

    public void setContent(List 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>
//接收时,使用我们的RestPageImpl
@RequestMapping(value="findPaged",method = RequestMethod.POST)
    public ApiResult> findPaged(@RequestBody ApiRequest requestVO)
    {
        String json= JsonConvert.toJSON(requestVO);
        HttpEntity request = new HttpEntity<>(json);
        ApiResult> result=restTemplate.exchange(
                webAPIKOBUrl+"api/findPaged"
                , HttpMethod.POST
                ,request
                ,new ParameterizedTypeReference>>(){}).getBody();
        return result;
    }

你可能感兴趣的:(ORM,spring,spring,boot,maven)