我尝试使用的有效负载看起来像
我有应该映射到JSON
上面有效负载的类列表。
InventoryPricePayload
@JsonIgnoreProperties(ignoreUnknown = true)
public class InventoryPricePayload {
private List<InventoryPriceDetail> inventoryPriceDetails;
@SuppressWarnings({ "RedundantNoArgConstructor", "UnusedDeclaration" })
public InventoryPricePayload() {
// used by Jackson
}
@Nonnull
public List<InventoryPriceDetail> getInventoryPriceDetails() {
return Collections.unmodifiableList(inventoryPriceDetails);
}
public void setInventoryPriceDetails(@Nonnull final List<InventoryPriceDetail> inventoryPriceDetails) {
//noinspection AssignmentToCollectionOrArrayFieldFromParameter
this.inventoryPriceDetails = inventoryPriceDetails;
}
}
然后InventoryPriceDetail
作为
@JsonIgnoreProperties(ignoreUnknown = true)
public class InventoryPriceDetail {
private List<InvListPriceData> invListPriceData;
@SuppressWarnings({ "RedundantNoArgConstructor", "UnusedDeclaration" })
public InventoryPriceDetail() {
// used by Jackson
}
@Nonnull
public List<InvListPriceData> getInvListPriceData() {
return Collections.unmodifiableList(invListPriceData);
}
public void setInvListPriceData(@Nonnull final List<InvListPriceData> invListPriceData) {
//noinspection AssignmentToCollectionOrArrayFieldFromParameter
this.invListPriceData = invListPriceData;
}
}
最后InvListPriceData
作为
@JsonIgnoreProperties(ignoreUnknown = true)
public class InvListPriceData {
private BigInteger total;
private BigInteger available;
private BigInteger bookable;
@SuppressWarnings({ "UnusedDeclaration", "RedundantNoArgConstructor" })
public InvListPriceData() {
// used by Jackson
}
@Nonnull
public BigInteger getTotal() {
return total;
}
public void setTotal(@Nonnull final BigInteger total) {
this.total = total;
}
@Nonnull
public BigInteger getAvailable() {
return available;
}
public void setAvailable(@Nonnull final BigInteger available) {
this.available = available;
}
@Nonnull
public BigInteger getBookable() {
return bookable;
}
public void setBookable(@Nonnull final BigInteger bookable) {
this.bookable = bookable;
}
}
我试着test
这样
final ObjectMapper mapper = new ObjectMapper();
try {
final InventoryPricePayload inventoryPricePayload = mapper.readValue(new File(getClass().getResource("/getInventoryAndPrice.json").getPath()), InventoryPricePayload.class);
System.out.println(inventoryPricePayload);
} catch (final IOException e) {
throw new RuntimeException("can not read resource :" + e.getMessage());
}
我得到如下失败:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.742 sec <<< FAILURE! - in com.org.my_services.external_services.ifs.IFSManagerTest
testMockIfsClient(com.org.my_services.external_services.ifs.IFSManagerTest) Time elapsed: 0.615 sec <<< ERROR!
java.lang.RuntimeException: can not read resource :Can not deserialize instance of com.org.my_services.external_services.ifs.converter.InventoryPricePayload out of START_ARRAY token
at [Source: /Users/harith/IdeaProjects/my_services/external_services/ifs/target/test-classes/getInventoryAndPrice.json; line: 1, column: 1]
at com.org.my_services.external_services.ifs.MockIfsClient.getInventoryAndPrice(MockIfsClient.java:25)
at com.org.my_services.external_services.ifs.IFSManager.getInventoryAndPrice(IFSManager.java:20)
at com.org.my_services.external_services.ifs.IFSManagerTest.testMockIfsClient(IFSManagerTest.java:28)
问题
我到底在哪里搞砸了?