使用jackson fasterxml将cucumber datatable转换为pojo时找不到字段

4uqofj5v  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(281)

我正在尝试使用xml将下面的datatable转换为pojo类,但是当我的测试运行时,我得到以下错误。我不知道为什么表没有Map。这个 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;
    }
                           .....
                           .....
                           .....
                           .....
                           .....
}
lf3rwulv

lf3rwulv1#

的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);

相关问题