无法反序列化对象的实例出来START_ARRAY令牌春季Webservice的 [英] Cannot deserialize instance of object out of START_ARRAY token in Spring Webservice
问题描述
我中号目前有连接到我的web服务在Android上的麻烦。我用杰克逊核心/数据绑定/注解2.2.4和Spring RESTWebService。如果我从浏览器访问URL,我可以看到JSON响应:(服务器返回目录\店\样子:)
I'm m currently having trouble connecting to my webservice on android. I use jackson-core/databind/annotation-2.2.4 and Spring RESTWebService. If I access the URL from browser I can see the JSON response: (server return List\Shop\ looks like:)
[{"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]}]
从客户终端(Android应用程序),我收到此错误信息:
From a Client endpoint (Android application) I receive this error message:
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.jackson.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 MappingJackson2HttpMessageConverter());
ShopContainer response = restTemplate.getForObject(url, ShopContainer.class);
在这里ShopContainer是:
where ShopContainer is:
public class ShopContainer {
private List<Shop> shops;
的店,地址和鞋的结构是(我想念这里的getter和setter):
structure of Shop, Address and Shoe is (I miss here getters and setters):
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 look around here and google but still can't figure out what I am missing at this point Any response would be greatly helpful.
问候。
@UPDATE
我修复JSON格式在服务器RequestMetod.GET使用杰克逊ObjectMaper。现在,它返回一个字符串。
I fix JSON format by using jackson ObjectMaper in server RequestMetod.GET. Now it return a String.
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控制台的样子:
JSON in console looks like:
[ {
"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.
推荐答案
您JSON包含数组,但你试图解析它作为对象。
出现此错误的原因对象必须从启动{
。
Your json contains an array, but you trying to parse it as object.
This error occurs because object must start from {
.
您有2个选项:
-
您可以摆脱
ShopContainer
类,并使用商店[]
而不是
ShopContainer response = restTemplate.getForObject(
url, ShopContainer.class);
替换
Shop[] response = restTemplate.getForObject(url, Shop[].class);
,然后让物体形成,或任何你需要的。
and then make object form it or whatever you need.
您可以更改您的服务器返回的对象,而不是列表
You can change your server to return object instead of list
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
替换
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
new ShopContainer(list));
这篇关于无法反序列化对象的实例出来START_ARRAY令牌春季Webservice的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!