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?
1 回答

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