Java 新手。在我的項(xiàng)目中,我通過 findAll(spec) 獲取數(shù)據(jù),如下所示:public interface ProductRepository extends JpaRepository<Product, Long> { List<Product> findAll(Specification<Product> spec);在控制器中,我將響應(yīng)轉(zhuǎn)換為 DTO,如下所示:(ProductResponse 是一個(gè) 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ù),所以我更改了存儲(chǔ)庫:public interface ProductRepository extends PagingAndSortingRepository<Product, Long> { List<Product> findAll(Specification<Product> spec, Pageable pageable);現(xiàn)在我收到以下錯(cuò)誤: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的方法怎么會(huì)返回這樣的響應(yīng)?如何從此響應(yīng)中獲取產(chǎn)品列表并將其轉(zhuǎn)換為 DTO?
1 回答

慕少森
TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
鑒于 HTMLUnit 在 JavaScript 方面存在的問題,您需要找到解決方法。知道你想要哪個(gè)元素后,你可以實(shí)現(xiàn)一個(gè) while 循環(huán)。這可能看起來像這樣:
while(!page.asText().contains(?<div id=\?exmaple-id\">“)){ webClient.waitForBackgroundJavaScript(500); }
如果你害怕陷入這個(gè)循環(huán),你可以在 while 條件中添加一個(gè)計(jì)數(shù)變量。就我的經(jīng)驗(yàn)而言,這是處理這種延遲的可靠方法嗎?
添加回答
舉報(bào)
0/150
提交
取消