无法从 START_OBJECT 令牌反序列化 `java.lang.String` 的实例

rob*_*e70 6 java fasterxml spring-boot

我收到以下错误消息,有人可以帮忙或建议如何最好地调试它。

无法java.lang.String在 [Source: (PushbackInputStream);反序列化超出 START_OBJECT 令牌的实例;行:1,列:37610](通过参考链:com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price [“现在”])

我正在尝试从 REst API 调用反序列化 Products 对象。代码一直运行良好,直到我添加了反序列化 Price 子类的代码。这看起来如下,

"price": {
        "was": "",
        "then1": "",
        "then2": "",
        "now": "59.00",
        "uom": "",
        "currency": "GBP"
      },
Run Code Online (Sandbox Code Playgroud)

我的价格 pojo 如下所示,

public class Price {

     @JsonProperty("was")
     String was;
     @JsonProperty("then1")
     String then1;
     @JsonProperty("then2")
     String then2;
     @JsonProperty("now")
     String now;
     @JsonProperty("uom")
     String uom;
     @JsonProperty("currency")
     String currency;

     public Price() {
         //blank constructor for JSON
     }

     @Override
     public String toString() {
         return "Price{" +
                 "was='" + was + '\'' +
                 ", then1='" + then1 + '\'' +
                 ", then2='" + then2 + '\'' +
                 ", now='" + now + '\'' +
                 ", uom='" + uom + '\'' +
                 ", currency='" + currency + '\'' +
                 '}';
     }
}
Run Code Online (Sandbox Code Playgroud)

我已经编写了一个 Junit 测试来尝试模拟错误,但它在我的测试中有效,

    @Test
    public void shouldConvertJsonProductListIntoPrice() {
        ObjectMapper objectMapper = new ObjectMapper();
        String content3 = "{\"products\": [{\"productId\": \"3525085\",\"title\": \"hush Tasha Vest Dress\", " +
                "\"price\": {\"was\": \"\",\"then1\": \"\",\"then2\": \"\",\"now\": \"59.00\",\"uom\": \"\",\"currency\": \"GBP\"}, " +
                "\"colorSwatches\": [{\"basicColor\": \"Red\",\"skuId\": \"237494589\"},{\"basicColor\": \"Blue\",\"skuId\": \"237494562\"}] " +
                "}]}";
        JavaType valueType = objectMapper.constructType(ProductList.class);
        ProductList readValue;
        try {
            readValue = objectMapper.readValue(content3, valueType);
            assertEquals("3525085", readValue.getProductList().get(0).productId);
            assertEquals("hush Tasha Vest Dress", readValue.getProductList().get(0).title);
            assertEquals("", readValue.getProductList().get(0).price.then1);
            assertEquals("59.00", readValue.getProductList().get(0).price.now);
            assertEquals("Blue", readValue.getProductList().get(0).colorSwatches[1].basicColor);
            assertEquals("237494562", readValue.getProductList().get(0).colorSwatches[1].skuId);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我注释掉“now”字段,那么我的 RestAPI 调用就可以正常工作,并且我看不到异常。所以似乎“现在”字段有问题,在这里我注意到它试图将“59.00”转换为字符串。这可能是 Fasterxml 转换器的问题吗?我需要帮忙吗?

Product 类如下(尽管这是我从 API 调用中接收到的字段列表大大减少了)。

public class Product {

     @JsonProperty("productId")
     String productId;

     @JsonProperty("title")
     String title;

     @JsonProperty("colorSwatches")
     ColorSwatch [] colorSwatches;

     @JsonProperty("price")
     Price price;

     public Product(){
         // blank required for Jackson
     }

     public Product(String productId, String title, ColorSwatch[] colorSwatches, Price price){
         this.productId = productId;
         this.title = title;
         this.colorSwatches = colorSwatches;
         this.price = price;
  }
Run Code Online (Sandbox Code Playgroud)

Loï*_*yen 13

错误指出它需要一个VALUE(最好是VALUE_STRING),而它得到一个START_OBJECT,所以你的问题可能来自表单的 json

"price": {
    "was": "",
    "then1": "",
    "then2": "",
    "now":  {
        ...
    }
    "uom": "",
    "currency": "GBP"
},
Run Code Online (Sandbox Code Playgroud)

而不是"now": "some value"代码期望的形式。