java 不能反序列化_不能将“Java.Lang.Studio”实例反序列化到StaskObl对象令牌中
我收到下面的錯誤消息,有人能幫助或建議如何最好地調試它嗎?
無法反序列化的實例
java.lang.String
超出起始值的對象
[來源:(pushbackinputstream)處的令牌;行:1,列:37610]
(通過參考鏈:
CopyToList[[產品] ] -gt;java. U.L.AyayList[ 23 ] -&G.M.Mult.產品[“價格”] -& com;COM.價格(“現在”))
我正在嘗試從RESTAPI調用反序列化產品對象。在我添加代碼來反序列化price子類之前,代碼一直工作得很好。如下所示:
"price": {
"was": "",
"then1": "",
"then2": "",
"now": "59.00",
"uom": "",
"currency": "GBP"
},
我的價格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 + '\'' +
'}';
}
}
我編寫了一個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();
}
}
如果我對“now”字段進行注釋,那么我的restapi調用工作得很好,我看不到異常。
所以看起來“now”字段有問題,我注意到它試圖將“59.00”轉換為字符串。這可能是fasterXML轉換器的問題嗎?我需要幫忙嗎?
產品類如下(盡管這是我從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;
}
總結
以上是生活随笔為你收集整理的java 不能反序列化_不能将“Java.Lang.Studio”实例反序列化到StaskObl对象令牌中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java string 数据结构_数据结
- 下一篇: java美元兑换,(Java实现) 美元