使用 Jackson Fasterxml 将 Cucumber DataTable 转换为 POJO 时找不到字段

Cant find field when converting Cucumber DataTable to POJO using Jackson Fasterxml

我正在尝试使用 Jackson Faster XML 将以下数据table 转换为 POJO class,但是当我的测试运行时出现以下错误。我不确定为什么 table 没有映射。 customerId 出现在 POJO class.

错误:

cucumber.runtime.CucumberException: No such field learning.pojo.customerId

步骤特征:

  And the user imported the following product file
      | customerId   | ....
      |customer1     | ....

步骤 Java:

@When("^the user imported the following product file$")
public void uploadFile(DataTable table) throws IOException {
     Example productImportFileModel = table.asList(Example.class).get(0);

根 POJO:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
                           "products"
                   })
public class Example{
    
    @JsonProperty("products")
    private List<Products> products = null;

    @JsonProperty("products")
    public List<Products> getProducts() {
        return products;
    }

    @JsonProperty("products")
    public void setProducts(List<Products> products) {
        this.products = products;
    }
}

POJO 产品:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
                           "customerId",
                           .....
                           .....
                           .....
                           .....
                           .....

                   })
public class Products {
    
    @JsonProperty("customerId")
    private String customerId;

    @JsonProperty("customerId")
    public String getCustomerId() {
        return customerId;
    }
    
    @JsonProperty("customerId")
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
                           .....
                           .....
                           .....
                           .....
                           .....
}

Example的json表示为:

{
  "products": [ ..... ]
}

Products的json表示为:

{
  "customerId": "customer1"
   ....
}

table 作为列表的 json 表示是:

[ 
  {
    "customerId": "customer1"
     ....
  }
]

所以考虑使用:

Product productImportFileModel = table.asList(Product.class).get(0);

或者更好:

@When("^the user imported the following product file$")
public void uploadFile(List<Product> products) throws IOException {
     Product productImportFileModel = products.get(0);

您可以通过向 Jackson 询问 json 表示来调试它:

JsonNode json = table.asList(JsonNode.class).get(0);
System.out.println(json);