无法从 Spring Webservice 中的 START_ARRAY 令牌反序列化对象实例 [英] Cannot deserialize instance of object out of START_ARRAY token in Spring Webservice

查看:38
本文介绍了无法从 Spring Webservice 中的 START_ARRAY 令牌反序列化对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前无法在 android 上连接到我的网络服务.我使用 jackson-core/databind/annotation-2.2.4 和 Spring RESTWebService.如果我从浏览器访问 URL,我可以看到 JSON 响应:(服务器返回 ListShop 看起来像 :)

I'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 the browser I can see the JSON response: (server return ListShop 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

我的服务器请求

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;

Shop、Address 和 Shoe 的结构是:(我省略了 getter 和 setter):

structure of Shop, Address and Shoe is : (I've omitted 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'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.

问候.

@更新

我通过使用 jackson 的 ObjectMapper 和 RequestMethod.GET 修复了 JSON.它现在返回一个字符串.

I've fixed the JSON by using jackson's ObjectMapper with RequestMethod.GET. It now returns 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're trying to parse it as an object. This error occurs because objects must start with {.

您有两个选择:

  1. 你可以去掉 ShopContainer 类并使用 Shop[] 代替

ShopContainer response  = restTemplate.getForObject(
    url, ShopContainer.class);

替换为

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);

替换为

return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
    new ShopContainer(list));

这篇关于无法从 Spring Webservice 中的 START_ARRAY 令牌反序列化对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆