如何使用 Spring RestTemplate 使用 Page<Entity> 响应

我正在使用spring data(mongoDb),我有我的存储库:

public interface StoriesRepository extends PagingAndSortingRepository<Story, String> {}

然后我有一个控制器:

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Page<StoryResponse>> getStories(Pageable pageable) {
    Page<StoryResponse> stories = storiesRepository.findAll(pageable).map(StoryResponseMapper::toStoryResponse);
    return ResponseEntity.ok(stories);
}

一切正常,但我无法使用 RestTemplate getForEntity 方法使用我的终结点:

def entity = restTemplate.getForEntity(getLocalhost("/story"), new TypeReference<Page<StoryResponse>>(){}.class)

我应该提供什么类才能成功反序列化我的实体页面?


答案 1
new TypeReference<Page<StoryResponse>>() {}

此语句的问题在于 Jackson 无法实例化抽象类型。您应该向 Jackson 提供有关如何使用具体类型实例化的信息。但是它的具体类型 ,没有默认构造函数或任何 s,因此您也不能使用以下代码:PagePageImpl@JsonCreator

new TypeReference<PageImpl<StoryResponse>>() {}

由于您无法将所需的信息添加到类中,因此最好为具有默认无参数构造函数的接口创建自定义实现,如本答案所示。然后在类型引用中使用该自定义实现,如下所示:PagePage

new TypeReference<CustomPageImpl<StoryResponse>>() {}

以下是从链接问题复制的自定义实现:

public class CustomPageImpl<T> extends PageImpl<T> {
    private static final long serialVersionUID = 1L;
    private int number;
    private int size;
    private int totalPages;
    private int numberOfElements;
    private long totalElements;
    private boolean previousPage;
    private boolean firstPage;
    private boolean nextPage;
    private boolean lastPage;
    private List<T> content;
    private Sort sort;

    public CustomPageImpl() {
        super(new ArrayList<>());
    }

    @Override
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    @Override
    public int getNumberOfElements() {
        return numberOfElements;
    }

    public void setNumberOfElements(int numberOfElements) {
        this.numberOfElements = numberOfElements;
    }

    @Override
    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public boolean isPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(boolean previousPage) {
        this.previousPage = previousPage;
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public void setFirstPage(boolean firstPage) {
        this.firstPage = firstPage;
    }

    public boolean isNextPage() {
        return nextPage;
    }

    public void setNextPage(boolean nextPage) {
        this.nextPage = nextPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public void setLastPage(boolean lastPage) {
        this.lastPage = lastPage;
    }

    @Override
    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    @Override
    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    public Page<T> pageImpl() {
        return new PageImpl<>(getContent(), new PageRequest(getNumber(),
                getSize(), getSort()), getTotalElements());
    }
}

答案 2

我知道这个线程有点旧,但希望有人能从中受益。

@Ali Dehghani的答案很好,除了它重新实现了已经完成的事情。我认为这是不必要的。通过创建一个扩展和指定构造函数的类,我找到了更好的解决方案:PageImpl<T>PageImpl<T>@JsonCreator

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.company.model.HelperModel;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.List;

public class HelperPage extends PageImpl<HelperModel> {

    @JsonCreator
    // Note: I don't need a sort, so I'm not including one here.
    // It shouldn't be too hard to add it in tho.
    public HelperPage(@JsonProperty("content") List<HelperModel> content,
                      @JsonProperty("number") int number,
                      @JsonProperty("size") int size,
                      @JsonProperty("totalElements") Long totalElements) {
        super(content, new PageRequest(number, size), totalElements);
    }
}

然后:

HelperPage page = restTemplate.getForObject(url, HelperPage.class);

这与创建类相同,但允许我们利用 中已有的所有代码。CustomPageImpl<T>PageImpl<T>