亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

org.springframework.data.domain.PageImpl 無法轉換為

org.springframework.data.domain.PageImpl 無法轉換為

飲歌長嘯 2023-08-09 15:20:10
Java 新手。在我的項目中,我通過 findAll(spec) 獲取數據,如下所示:public interface ProductRepository extends JpaRepository<Product, Long> {    List<Product> findAll(Specification<Product> spec);在控制器中,我將響應轉換為 DTO,如下所示:(ProductResponse 是一個 DTO)private List<ProductResponse> convertProductListToResponse(List<Product> products) {    List<ProductResponse> productResponseList = new ArrayList<ProductResponse>();    for(int i = 0; i < products.size(); i++) {        ProductResponse productResponse = new ProductResponse();            productResponse.convert(products.get(i));            productResponseList.add(productResponse);    }    return productResponseList;}@PostMapping("getProducts/{page}")public List<ProductResponse> getAllProducts(@PathVariable("page") int page) {    ProductSpecification nameSpecification = new ProductSpecification(new SearchCriteria("title", ":", "First Product"));    // Service simply uses repository method:    List<Product> filterProducts = productService.findAll(Specification.where(nameSpecification));    List<ProductResponse> productResponseList = this.convertProductListToResponse(filterProducts);    return productResponseList;}然后我決定通過分頁獲取數據,所以我更改了存儲庫:public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {     List<Product> findAll(Specification<Product> spec, Pageable pageable);現在我收到以下錯誤:java.lang.ClassCastException: org.springframework.data.domain.PageImpl cannot be cast to com.vendo.app.entity.Product然后我直接在控制器中輸出響應(filterProducts),并發現響應結構如下:[ { "content": [        { "id": 1, "deleted": false, "title": "First Product", ...        ....// array of product objects我實在不明白,響應類型為List的方法怎么會返回這樣的響應?如何從此響應中獲取產品列表并將其轉換為 DTO?
查看完整描述

3 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

基本上默認findAll有 2 個變體

https://img1.sycdn.imooc.com//64d33e4f000148e110010397.jpg


如果您想轉換Page<Product>為簡單List<Product>

productRepository.findAll(PageRequest.of(0, count)).toList();


查看完整回答
反對 回復 2023-08-09
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

findAll方法中你應該返回Page<Product>而不是List<Product>



查看完整回答
反對 回復 2023-08-09
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

我意識到我的錯誤是使用 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;
}


查看完整回答
反對 回復 2023-08-09
  • 3 回答
  • 0 關注
  • 383 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號