第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

org.springframework.data.domain.PageImpl 無法轉(zhuǎn)換為

org.springframework.data.domain.PageImpl 無法轉(zhuǎn)換為

飲歌長嘯 2023-08-09 15:20:10
Java 新手。在我的項目中,我通過 findAll(spec) 獲取數(shù)據(jù),如下所示:public interface ProductRepository extends JpaRepository<Product, Long> {    List<Product> findAll(Specification<Product> spec);在控制器中,我將響應(yīng)轉(zhuǎn)換為 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;}然后我決定通過分頁獲取數(shù)據(jù),所以我更改了存儲庫:public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {     List<Product> findAll(Specification<Product> spec, Pageable pageable);現(xiàn)在我收到以下錯誤:java.lang.ClassCastException: org.springframework.data.domain.PageImpl cannot be cast to com.vendo.app.entity.Product然后我直接在控制器中輸出響應(yīng)(filterProducts),并發(fā)現(xiàn)響應(yīng)結(jié)構(gòu)如下:[ { "content": [        { "id": 1, "deleted": false, "title": "First Product", ...        ....// array of product objects我實(shí)在不明白,響應(yīng)類型為List的方法怎么會返回這樣的響應(yīng)?如何從此響應(yīng)中獲取產(chǎn)品列表并將其轉(zhuǎn)換為 DTO?
查看完整描述

3 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊

基本上默認(rèn)findAll有 2 個變體

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


如果您想轉(zhuǎn)換Page<Product>為簡單List<Product>

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


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

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個贊

findAll方法中你應(yīng)該返回Page<Product>而不是List<Product>



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

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個贊

我意識到我的錯誤是使用 List 而不是 Page 作為 findAll 的返回類型。

存儲庫應(yīng)如下所示:

??Page<Product>?findAll(Specification<Product>?spec,?Pageable?pageable);

控制器應(yīng)如下所示:

????@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;
}


查看完整回答
反對 回復(fù) 2023-08-09
  • 3 回答
  • 0 關(guān)注
  • 394 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號