Note that there are some explanatory texts on larger screens.

plurals
  1. POJsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY
    primarykey
    data
    text
    <p>I'm using Spring's <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/client/RestTemplate.html" rel="nofollow">RestTemplate</a> to consume the <a href="http://docs.chargify.com/api-transactions" rel="nofollow">Chargify API</a>, using JSON as the payload format. It has been going pretty smoothly, however when I try to GET an array of objects it fails miserably. For example, consider the following:</p> <pre><code>Transaction[] transactions = restTemplate.getForObject( CHARGIFY_ENDPOINT + "/subscriptions/{subscription}/transactions.json", Transaction[].class, subscriptionId ); </code></pre> <p>The actual GET goes thru fine, with a JSON response of:</p> <pre><code>[ { "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" } } ] </code></pre> <p>However, when Jackson attempts to deserialize the JSON, it results in <code>JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY</code>:</p> <pre><code>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 - &lt;&lt; "[{"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] </code></pre> <p>I ran into this issue before with Jackson, and after spending a couple days of research I ended up going a different route. That is not an option this time, and I'm having no luck figuring out a solution.</p> <p>Any ideas? I don't care if I get an array or a <code>List</code> back. I haven't had luck with either.</p> <p>While reading Jackson's docs, I saw where you should be able to wrap the list, i.e. <code>new TypeReference&lt;List&lt;Transaction&gt;&gt;()</code>, but I had no luck there either.</p> <p>BTW, because of how Chargify formats their JSON responses my RestTemplate is configured with a custom <a href="http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/ObjectMapper.html" rel="nofollow"><code>ObjectMapper</code></a> that sets these options:</p> <pre><code>configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true); configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); </code></pre> <p>When I remove these options, no <code>JsonMappingException</code> is thrown however all of the fields of the resulting <code>Transaction</code> objects are null.</p> <p>I also tried adding this option, but it does not seem to help:</p> <pre><code>configure(DeserializationConfig.Feature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload