一尘不染

无法在Spring Webservice中从START_ARRAY令牌中反序列化对象的实例

json

我目前无法在android上连接到我的网络服务。我使用jackson-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]}]

从客户端端点(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.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在哪里:

public class ShopContainer {
   private List<Shop> shops;

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

我已经在这里和Google上进行了搜索,但仍然无法弄清我目前所缺少的内容。

任何回应将大有帮助。

问候。

@更新

我已经通过将Jackson的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如下所示:

[ {
  "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 ]
  } ]
} ]

请求仍然不起作用-错误是相同的。


阅读 334

收藏
2020-07-27

共1个答案

一尘不染

您的json包含一个数组,但是您尝试将其解析为一个对象。发生此错误的原因是对象必须以开头{

您有2个选择:

  1. 您可以摆脱ShopContainer该类而Shop[]改用
    ShopContainer response  = restTemplate.getForObject(
    url, ShopContainer.class);
    

用。。。来代替

    Shop[] response  = restTemplate.getForObject(url, Shop[].class);

然后从中制作所需的对象。

  1. 您可以更改服务器以返回对象而不是列表
    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
    

用。。。来代替

    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
    new ShopContainer(list));
2020-07-27