我正在使用spring数据(mongoDb),我有我的存储库:
public interface StoriesRepository extends PagingAndSortingRepository{}
然后我有一个控制器:
@RequestMapping(method = RequestMethod.GET) public ResponseEntity> getStories(Pageable pageable) { Page stories = storiesRepository.findAll(pageable).map(StoryResponseMapper::toStoryResponse); return ResponseEntity.ok(stories); }
一切正常,但我无法使用RestTemplate getForEntity方法使用我的端点:
def entity = restTemplate.getForEntity(getLocalhost("/story"), new TypeReference>(){}.class)
我应该提供什么类来成功反序列化我的实体页面?
new TypeReference>() {}
这个陈述的问题是Jackson无法实例化抽象类型.您应该向Jackson提供有关如何Page
使用具体类型进行实例化的信息.但它的具体类型PageImpl
没有默认构造函数或任何@JsonCreator
s,所以你不能使用以下代码:
new TypeReference>() {}
由于您无法将所需信息添加到Page
类中,因此最好为Page
具有默认无参数构造函数的接口创建自定义实现,如本答案中所述.然后在类型引用中使用该自定义实现,如下所示:
new TypeReference>() {}
以下是从链接问题复制的自定义实现:
public class CustomPageImplextends PageImpl { 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 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 getContent() { return content; } public void setContent(List content) { this.content = content; } @Override public Sort getSort() { return sort; } public void setSort(Sort sort) { this.sort = sort; } public Page pageImpl() { return new PageImpl<>(getContent(), new PageRequest(getNumber(), getSize(), getSort()), getTotalElements()); } }
我知道这个帖子有点旧,但希望有人会从中受益.
@Ali Dehghani的答案很好,除了它重新实现PageImpl
已经完成的工作.我认为这是相当不必要的.我通过创建一个扩展PageImpl
并指定@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{ @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 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
类相同,但允许我们利用已经存在的所有代码PageImpl
.
作为"探路者"提到你可以使用exchange
方法RestTemplate
.然而,ParameterizedTypeReference
不应该通过你应该通过ParameterizedTypeReference
.当您收到回复时,您可以检索内容 - Collection
.
代码应该如下所示:
ResponseEntity> response = restTemplate.exchange(getLocalhost("/story"), HttpMethod.GET, null, new ParameterizedTypeReference >() {}); PagedResources storiesResources = response.getBody(); Collection stories = storiesResources.getContent();
除了内容storiesResources
还保存页面元数据和链接.
这里有一个更详细的解释:https://stackoverflow.com/a/46847429/8805916