0

我尝试使用的有效负载看起来像

在此处输入图像描述

我有应该映射到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)

问题

我到底在哪里搞砸了?

4

2 回答 2

0

根据 Jackson文档(请参阅Data Binding with Generics),我做了以下事情,一切都很好

final List<InventoryPriceDetail> inventoryPriceDetails= mapper.readValue(new File(getClass().getResource("/getInventoryAndPrice.json").getPath()), new TypeReference<List<InventoryPriceDetail>>() {});
于 2014-05-07T03:55:37.143 回答
0

而不是使用

new TypeReference<List<InventoryPriceDetail>>() {}

这有点难看,因为您也可以使用类实现

mapper.getTypeFactory().constructCollectionType(List.class, InventoryPriceDetail.class)

于 2014-05-07T06:01:39.087 回答