Android 无法从 Spring Webservice 中的 START_ARRAY 令牌反序列化对象实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25510083/
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
Cannot deserialize instance of object out of START_ARRAY token in Spring Webservice
提问by Daram
I'm currently having trouble connecting to my webservice on android. I use Hymanson-core/databind/annotation-2.2.4 and Spring RESTWebService. If I access the URL from the browser I can see the JSON response: (server return List\Shop\ looks like:)
我目前在 android 上连接到我的网络服务时遇到问题。我使用 Hymanson-core/databind/annotation-2.2.4 和 Spring RESTWebService。如果我从浏览器访问 URL,我可以看到 JSON 响应:(服务器返回 List\Shop\ 看起来像:)
[{"name":"shopqwe","mobiles":[],"address":{"town":"city",
"street":"streetqwe","streetNumber":"59","cordX":2.229997,"cordY":1.002539},
"shoe" [{"shoeName":"addidas","number":"631744030","producent":"nike","price":999.0,
"sizes":[30.0,35.0,38.0]}]
From a Client endpoint (Android application) I receive this error message:
我从客户端端点(Android 应用程序)收到此错误消息:
08-26 17:43:07.406: E/AllShopsAsyc(28203): Could not read JSON: Can not deserialize
instance of com.auginzynier.data.ShopContainer out of START_ARRAY token
08-26 17:43:07.406: E/AllShopsAsyc(28203): at [Source:
com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream@41efbd48; line: 1,
column: 1]; nested exception is com.fasterxml.Hymanson.databind.JsonMappingException:
Can not deserialize instance of com.auginzynier.data.ShopContainer out of START_ARRAY
token
08-26 17:43:07.406: E/AllShopsAsyc(28203): at [Source:
com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream@41efbd48; line: 1,
column: 1]
08-26 17:43:07.406: E/AllShopsAsyc(28203):
org.springframework.http.converter.HttpMessageNotReadableException: Could not read
JSON: Can not deserialize instance of com.auginzynier.data.ShopContainer out of
START_ARRAY token
My server request
我的服务器请求
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingHymanson2HttpMessageConverter());
ShopContainer response = restTemplate.getForObject(url, ShopContainer.class);
where ShopContainer is:
ShopContainer 在哪里:
public class ShopContainer {
private List<Shop> shops;
structure of Shop, Address and Shoe is : (I've omitted getters and setters):
Shop、Address 和 Shoe 的结构是:(我省略了 getter 和 setter):
public class Shop {
@JsonProperty("name") private String name;
@JsonProperty("mobiles") private List<String> mobiles = new ArrayList<String>();
@JsonProperty("address") private Address address;
@JsonProperty("shoe") private List<Shoe> shoe = new ArrayList<Shoe>();
public class Address {
@JsonProperty("town") private String town;
@JsonProperty("street") private String street;
@JsonProperty("streetNumber") private String streetNumber;
@JsonProperty("cordX") private Double cordX;
@JsonProperty("cordY") private Double cordY;
public class Shoe {
@JsonProperty("shoeName") private String shoeName;
@JsonProperty("number") private String number;
@JsonProperty("producent") private String producent;
@JsonProperty("price") private Double price;
@JsonProperty("sizes") private List<Double> sizes = new ArrayList<Double>();
I've look here and on google but still can't figure out what I am missing at this point.
我在这里和谷歌上看过,但仍然无法弄清楚此时我错过了什么。
Any response would be greatly helpful.
任何回应都会非常有帮助。
Regards.
问候。
@UPDATE
@更新
I've fixed the JSON by using Hymanson's ObjectMapper with RequestMethod.GET. It now returns a String.
我已经通过使用 Hymanson 的 ObjectMapper 和 RequestMethod.GET 修复了 JSON。它现在返回一个字符串。
list is List<Shop>
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("D:\Android\shop.json"), list);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list));
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
JSON in console looks like:
控制台中的 JSON 如下所示:
[ {
"name" : "shopqwe",
"mobiles" : [ ],
"address" : {
"town" : "city",
"street" : "streetqwe",
"streetNumber" : "59",
"cordX" : 2.229997,
"cordY" : 2.002539
},
"shoe" : [ {
"shoeName" : "addidas",
"number" : "631744033",
"producent" : "nike",
"price" : 10.0,
"sizes" : [ 30.0, 35.0, 38.0 ]
} ]
} ]
Request still doesn't work - error is the same.
请求仍然不起作用 - 错误是一样的。
回答by Ordon
Your json contains an array, but you're trying to parse it as an object.
This error occurs because objects must start with {
.
您的 json 包含一个数组,但您试图将其解析为一个对象。发生此错误是因为对象必须以{
.
You have 2 options:
您有 2 个选择:
You can get rid of the
ShopContainer
class and useShop[]
insteadShopContainer response = restTemplate.getForObject( url, ShopContainer.class);
replace with
Shop[] response = restTemplate.getForObject(url, Shop[].class);
and then make your desired object from it.
You can change your server to return an object instead of a list
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
replace with
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString( new ShopContainer(list));
您可以摆脱
ShopContainer
课程并Shop[]
改用ShopContainer response = restTemplate.getForObject( url, ShopContainer.class);
用。。。来代替
Shop[] response = restTemplate.getForObject(url, Shop[].class);
然后从中制作您想要的对象。
您可以更改服务器以返回对象而不是列表
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
用。。。来代替
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString( new ShopContainer(list));
回答by geoand
Taking for granted that the JSON you posted is actually what you are seeing in the browser, then the problem is the JSON itself.
假设您发布的 JSON 实际上是您在浏览器中看到的内容,那么问题在于 JSON 本身。
The JSON snippet you have posted is malformed.
您发布的 JSON 片段格式不正确。
You have posted:
您已发布:
[{
"name" : "shopqwe",
"mobiles" : [],
"address" : {
"town" : "city",
"street" : "streetqwe",
"streetNumber" : "59",
"cordX" : 2.229997,
"cordY" : 1.002539
},
"shoe"[{
"shoeName" : "addidas",
"number" : "631744030",
"producent" : "nike",
"price" : 999.0,
"sizes" : [30.0, 35.0, 38.0]
}]
while the correct JSON would be:
而正确的 JSON 是:
[{
"name" : "shopqwe",
"mobiles" : [],
"address" : {
"town" : "city",
"street" : "streetqwe",
"streetNumber" : "59",
"cordX" : 2.229997,
"cordY" : 1.002539
},
"shoe" : [{
"shoeName" : "addidas",
"number" : "631744030",
"producent" : "nike",
"price" : 999.0,
"sizes" : [30.0, 35.0, 38.0]
}
]
}
]