spring-mvc - JsonMappingException : Current token not START_OBJECT (needed to unwrap root name 'Transaction[]' ), 但 START_ARRAY

标签 spring-mvc jackson chargify

我正在使用 Spring 的 RestTemplate消耗 Chargify API ,使用 JSON 作为负载格式。它一直进行得非常顺利,但是当我尝试获取对象数组时,它惨败了。例如,请考虑以下情况:

Transaction[] transactions = restTemplate.getForObject(
        CHARGIFY_ENDPOINT + "/subscriptions/{subscription}/transactions.json",
        Transaction[].class,
        subscriptionId
    );

实际的 GET 运行良好,JSON 响应为:

[ { "transaction" : { "amount_in_cents" : 3006,
    "created_at" : "2012-06-17T16:32:05-04:00",
    "ending_balance_in_cents" : 0,
    "id" : 17283728,
    "kind" : null,
    "memo" : "",
    "payment_id" : null,
    "product_id" : 120387,
    "subscription_id" : 1947292,
    "success" : true,
    "transaction_type" : "payment",
    "type" : "Payment"
      } },
  { "transaction" : { "amount_in_cents" : 5900,
    "created_at" : "2012-06-17T16:32:05-04:00",
    "ending_balance_in_cents" : 3006,
    "id" : 17283727,
    "kind" : "baseline",
    "memo" : "Professional Plan (06/17/2012 - 07/17/2012)",
    "payment_id" : 17283728,
    "product_id" : 120387,
    "subscription_id" : 1947292,
    "success" : true,
    "transaction_type" : "charge",
    "type" : "Charge"
      } },
  { "transaction" : { "amount_in_cents" : -2894,
    "created_at" : "2012-06-17T16:32:03-04:00",
    "ending_balance_in_cents" : -2894,
    "id" : 17283726,
    "kind" : "prorated",
    "memo" : "",
    "payment_id" : null,
    "product_id" : 120387,
    "subscription_id" : 1947292,
    "success" : null,
    "transaction_type" : "adjustment",
    "type" : "Adjustment"
      } },
  { "transaction" : { "amount_in_cents" : 2900,
    "created_at" : "2012-06-17T15:17:07-04:00",
    "ending_balance_in_cents" : 0,
    "id" : 17281084,
    "kind" : null,
    "memo" : "",
    "payment_id" : null,
    "product_id" : 120386,
    "subscription_id" : 1947292,
    "success" : true,
    "transaction_type" : "payment",
    "type" : "Payment"
      } },
  { "transaction" : { "amount_in_cents" : 2900,
    "created_at" : "2012-06-17T15:17:06-04:00",
    "ending_balance_in_cents" : 2900,
    "id" : 17281083,
    "kind" : "baseline",
    "memo" : "Standard Plan (06/17/2012 - 07/17/2012)",
    "payment_id" : 17281084,
    "product_id" : 120386,
    "subscription_id" : 1947292,
    "success" : true,
    "transaction_type" : "charge",
    "type" : "Charge"
      } }
]

但是,当 Jackson 尝试反序列化 JSON 时,结果是 JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY :

16:59:12.651 [http-8080-1] DEBUG org.springframework.web.client.RestTemplate - GET request for "https://foobar.chargify.com/subscriptions/1947292/transactions.json" resulted in 200 (OK)
16:59:12.651 [http-8080-1] DEBUG org.springframework.web.client.RestTemplate - Reading [[Lcom.foobar.chargify.Transaction;] as "application/json;charset=utf-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@1ea8dbd]
16:59:12.662 [http-8080-1] DEBUG org.apache.http.wire - << "[{"transaction":{"type":"Payment","amount_in_cents":3006,"payment_id":null,"memo":"","id":17283728,"created_at":"2012-06-17T16:32:05-04:00","subscription_id":1947292,"kind":null,"ending_balance_in_cents":0,"success":true,"product_id":120387,"transaction_type":"payment"}},{"transaction":{"type":"Charge","amount_in_cents":5900,"payment_id":17283728,"memo":"Professional Plan (06/17/2012 - 07/17/2012)","id":17283727,"created_at":"2012-06-17T16:32:05-04:00","subscription_id":1947292,"kind":"baseline","ending_balance_in_cents":3006,"success":true,"product_id":120387,"transaction_type":"charge"}},{"transaction":{"type":"Adjustment","amount_in_cents":-2894,"payment_id":null,"memo":"","id":17283726,"created_at":"2012-06-17T16:32:03-04:00","subscription_id":1947292,"kind":"prorated","ending_balance_in_cents":-2894,"success":null,"product_id":120387,"transaction_type":"adjustment"}},{"transaction":{"type":"Payment","amount_in_cents":2900,"payment_id":null,"memo":"","id":17281084,"created_at":"2012-06-17T15:17:07-04:00","subscription_id":1947292,"kind":null,"ending_balance_in_cents":0,"success":true,"product_id":120386,"transaction_type":"payment"}},{"transaction":{"type":"Charge","amount_in_cents":2900,"payment_id":17281084,"memo":"Standard Plan (06/17/2012 - 07/17/2012)","id":17281083,"created_at":"2012-06-17T15:17:06-04:00","subscription_id":1947292,"kind":"baseline","ending_balance_in_cents":2900,"success":true,"product_id":120386,"transaction_type":"charge"}}]"
16:59:12.683 [http-8080-1] DEBUG org.apache.http.impl.conn.SingleClientConnManager - Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@17ed710
16:59:12.684 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public void com.foobar.controllers.TestController.viewTransactions(int)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
 at [Source: org.apache.http.conn.EofSensorInputStream@598a5d; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
 at [Source: org.apache.http.conn.EofSensorInputStream@598a5d; line: 1, column: 1]
16:59:12.686 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public void com.foobar.controllers.TestController.viewTransactions(int)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
 at [Source: org.apache.http.conn.EofSensorInputStream@598a5d; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
 at [Source: org.apache.http.conn.EofSensorInputStream@598a5d; line: 1, column: 1]
16:59:12.686 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public void com.foobar.controllers.TestController.viewTransactions(int)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
 at [Source: org.apache.http.conn.EofSensorInputStream@598a5d; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
 at [Source: org.apache.http.conn.EofSensorInputStream@598a5d; line: 1, column: 1]

我之前和 jackson 一起遇到过这个问题,经过几天的研究,我最终走了一条不同的路线。这次不行,而且我没能找到解决方案。

有什么想法吗?我不在乎我是否得到一个数组或 List后退。我也没有运气。

在阅读 Jackson 的文档时,我看到您应该能够在哪里包装列表,即 new TypeReference<List<Transaction>>() ,但我在那里也没有运气。

顺便说一句,由于 Chargify 如何格式化其 JSON 响应,我的 RestTemplate 配置了自定义 ObjectMapper 设置这些选项:

configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

当我删除这些选项时,没有 JsonMappingException然而,结果 Transaction 的所有字段都会被抛出。对象为空。

我也尝试添加此选项,但似乎没有帮助:

configure(DeserializationConfig.Feature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);

最佳答案

您只需要在 Transaction 之上使用另一个包装器类型,同时请删除 UNWRAP_ROOT_VALUE 选项。

类(class)将遵循以下原则:

public class TransactionHolder {
    private Transaction transaction;
... 
}

public class Transaction {
    private String amount_in_cents;
    private String created_at;
    private int ending_balance_in_cents;
    private int id;
    private String kind;
    private String memo;
    private int payment_id;
    private int product_id;
    private int subscription_id;
    private boolean success;
    private String transaction_type;
    private String type;
...
}

使用您的示例 json,以下内容对我来说很干净:

ObjectMapper mapper = new ObjectMapper();
InputStream is = this.getClass().getResourceAsStream("sample.json");
TransactionHolder[] holders = mapper.readValue(is,TransactionHolder[].class); 

关于spring-mvc - JsonMappingException : Current token not START_OBJECT (needed to unwrap root name 'Transaction[]' ), 但 START_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075218/

相关文章:

ruby-on-rails - 带有 chargify 的 cucumber 功能示例

java - Spring - 使用一个 Controller 处理多个表单提交

java - Spring mvc序列化结果如何与jackson并基于返回值的注释?

java - com.fasterxml.jackson.databind.JsonMappingException : Numeric value (1557587751202) out of range of int

java - 将 Json 解析为分区日期时间

ruby-on-rails-3 - 对于零:NilClass using chargify_api_ares gem未定义的方法 'path'

linux - Ant Tomcat : Failed to create task or type "list"

java - 类型 'org/springframework/http/MediaType'(当前帧,堆栈 [1])不可分配给 'org/springframework/util/MimeType'

java - Spring MVC 文件上传 - 仅在 IE 中 - 异步 IO 操作失败(1),原因 : RC: 10053