Warm tip: This article is reproduced from serverfault.com, please click

java-使用Jackson Fasterxml将Cucumber DataTable转换为POJO时无法找到字段

(java - Cant find field when converting Cucumber DataTable to POJO using Jackson Fasterxml)

发布于 2020-11-30 18:44:00

我想使用Jackson Faster XML将以下Datatable转换为POJO类,但是当我的测试运行时,出现以下错误。我不确定为什么表格没有映射。customerId存在于POJO类。

错误:

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;
    }
                           .....
                           .....
                           .....
                           .....
                           .....
}
Questioner
stuckAsFuk
Viewed
0
M.P. Korstanje 2020-12-01 07:31:03

的json表示Example为:

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

的json表示Products为:

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

以列表形式表示的表的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);