线程 "main" java.lang.ClassCastException 中的异常:www.logisense.com.pogos.wizardNewProduct.ProductWorkingDates 无法转换为 java.util.List

Exception in thread "main" java.lang.ClassCastException: www.logisense.com.pogos.wizardNewProduct.ProductWorkingDates cannot be cast to java.util.List

我正在尝试将对象转换为 JSON,我有以下 POGO classes:

import lombok.Data;
import java.util.List;

@Data
public class CreateNewProductRequest {

    private List<ProductIncludedTags> productIncludedTags;

    private List<ProductWorkingDates> productWorkingDates;

    private double longitude;

    private List<ProductTags> productTags;

}

import Lombok.Data;
import java.util.List;

@Data
public class ProductWorkingDates {

    private String fromDate;
    private String toDate;
    private String name;
    private Boolean strictHours;
    private List<TimeSlots> timeSlots;
    private String deletedAt;
    private Integer maxUsedTicketsQuantity;
    private  Integer errorCode;
}


import lombok.Data;

@Data
public class TimeSlots {

    private String startTime;
    private String endTime;
    private String duration;
    private String quantity;
    private String usedQuantity;
    private boolean active;
    private String deletedAt;
}

我正在通过 Cucumber 传递数据,我有以下场景

场景:提供商在 ProductWorkingDates 中输入数据

Given product Working Dates is set with following fields
  | fromDate        | toDate          | name                | strictHours |
  | Thu May 27 2021 | Sat Dec 31 2022 | 1234567sdfgh#$#%^%& | false       |

我有步骤 Defenition class,我试图通过 List<Map<String, String>> productWorkingDates;

    @Given("^product Working Dates is set with following fields$")
    public void productWorkingDatesIsSetWithFollowingFields(List<Map<String,String>> productWorkingDates) {
        ProductWorkingDates productWorkingDates1 = new ProductWorkingDates();
        productWorkingDates1.setFromDate(productWorkingDates.get(0).get("fromDate"));
        productWorkingDates1.setToDate(productWorkingDates.get(0).get("toDate"));
        productWorkingDates1.setName(productWorkingDates.get(0).get("name"));
        productWorkingDates1.setStrictHours(Boolean.parseBoolean(productWorkingDates.get(0).get("strictHours")));
       


//====> this is where I'm having an issue, pls help 

productRequest.setProductWorkingDates((List<ProductWorkingDates>) productWorkingDates1);
  

  }

据我所知,productWorkingDates1 不是列表,而是列表的一个元素,类型为 ProductWorkingDates : ProductWorkingDates productWorkingDates1 = new ProductWorkingDates();

当尝试从 ProductWorkingDates 转换为 List 时,您遇到了您所描述的异常: (List<ProductWorkingDates>) productWorkingDates1

你需要的是这样的东西:

List<ProductWorkingDates> listProductWorkingDates = new ArrayList<>();
listProductWorkingDates.add(productWorkingDates1);
productRequest.setProductWorkingDates(listProductWorkingDates);