Java 无法将org.springframework.data.domain.PageImpl强制转换为

Java 无法将org.springframework.data.domain.PageImpl强制转换为,java,spring-boot,jpa,Java,Spring Boot,Jpa,Java新手。在我的项目中,我通过findAll(spec)获得如下数据: public interface ProductRepository extends JpaRepository<Product, Long> { List<Product> findAll(Specification<Product> spec); [ { "content": [ { "id": 1, "deleted": false, "title":

Java新手。在我的项目中,我通过findAll(spec)获得如下数据:

public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findAll(Specification<Product> spec);
[ { "content": [
        { "id": 1, "deleted": false, "title": "First Product", ...
        ....// array of product objects
然后我直接在控制器中输出响应(filterProducts),发现响应的结构如下:

public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findAll(Specification<Product> spec);
[ { "content": [
        { "id": 1, "deleted": false, "title": "First Product", ...
        ....// array of product objects
我真的不明白,响应类型为List的方法怎么能返回这样的响应? 如何从该响应中获取产品列表并将其转换为DTO

谢谢。

在的帮助下,我意识到我的错误是使用List而不是Page作为findAll的返回类型

存储库应如下所示:

  Page<Product> findAll(Specification<Product> spec, Pageable pageable);
    @PostMapping("getProducts/{page}")
public List<ProductResponse> getAllProducts(@PathVariable("page") int page) {

    Pageable productPageable = PageRequest.of(0, page);

    ProductSpecification nameSpecification = new ProductSpecification(new SearchCriteria("title", ":", "First Product"));

    Page<Product> filterProducts = productService.findAll(Specification.where(nameSpecification), productPageable);

    List<ProductResponse> productResponseList = this.convertProductListToResponse(filterProducts.getContent());
    return productResponseList;
}
Page findAll(规范规范,可分页);
控制器应如下所示:

  Page<Product> findAll(Specification<Product> spec, Pageable pageable);
    @PostMapping("getProducts/{page}")
public List<ProductResponse> getAllProducts(@PathVariable("page") int page) {

    Pageable productPageable = PageRequest.of(0, page);

    ProductSpecification nameSpecification = new ProductSpecification(new SearchCriteria("title", ":", "First Product"));

    Page<Product> filterProducts = productService.findAll(Specification.where(nameSpecification), productPageable);

    List<ProductResponse> productResponseList = this.convertProductListToResponse(filterProducts.getContent());
    return productResponseList;
}
@PostMapping(“getProducts/{page}”)
公共列表getAllProducts(@PathVariable(“page”)int page){
Pageable productPageable=PageRequest.of(0,第页);
ProductSpecification nameSpecification=新的产品规格(新的搜索条件(“标题”,“第一个产品”);
Page filterProducts=productService.findAll(Specification.where(namesspecification),productPageable);
List productResponseList=this.convertProductListToResponse(filterProducts.getContent());
返回产品响应列表;
}

findAll
方法中,您应该返回
页面
,而不是
列表
基本上默认的
findAll
有两个变体

如果要将
页面
转换为
列表


你不应该在
findAll
方法中返回
Page
而不是
List
?天哪,谢谢你,你救了我一天。:)但是,当我在存储库中应用错误的类型转换时,为什么java没有警告我呢?