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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54062469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:58:26  来源:igfitidea点击:

Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

javaspring-bootfasterxml

提问by robbie70

I am getting the below error message, can someone please help or suggest how best to debug this.

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

Cannot deserialize instance of java.lang.Stringout of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 37610] (through reference chain: com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price["now"])

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

I am trying to deserialise a Products object from a REst API call. The code has been working fine until I added code to deserialise a Price sub-class. This looks as follows,

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

"price": {
        "was": "",
        "then1": "",
        "then2": "",
        "now": "59.00",
        "uom": "",
        "currency": "GBP"
      },

My Price pojo looks as follows,

我的价格 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 + '\'' +
                 '}';
     }
}

I have written a Junit test to try and simulate the error but it works in my test,

我已经编写了一个 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();
        }
    }

If I comment out the "now" field then my RestAPI call works perfectly and I dont see the exception.So it seems that there is a problem with the "now" field and here I notice that its trying to convert "59.00" into a String. Could this be a problem for the Fasterxml converter ? do I need to help it maybe ?

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

The Product class is as follows (although this is much reduced list of fields that I am receiving off the API call).

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 Hymanson
     }

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

采纳答案by Lo?c Le Doyen

The error states it expects a VALUE(VALUE_STRINGpreferably), whereas it gets a START_OBJECT, so your issue is probably coming from a json of the form

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

"price": {
    "was": "",
    "then1": "",
    "then2": "",
    "now":  {
        ...
    }
    "uom": "",
    "currency": "GBP"
},

Instead of the "now": "some value"form expected by the code.

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