我想使用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;
}
.....
.....
.....
.....
.....
}
的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);