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

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

HTMLUnit 未使用 JavaScript 返回完全加載的頁面

HTMLUnit 未使用 JavaScript 返回完全加載的頁面

鴻蒙傳說 2023-08-09 15:21:08
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 條件中添加一個計數變量。就我的經驗而言,這是處理這種延遲的可靠方法嗎?


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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