JsonMappingException:当前令牌不是 START_OBJECT(需要解开根名称“Transaction[]”),而是 START_ARRAY

2023-11-29

我正在使用Spring的休息模板来消耗充电接口,使用 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]

我之前和杰克逊遇到过这个问题,经过几天的研究,我最终走了一条不同的路线。这次这不是一个选择,而且我也没有找到解决方案。

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

在阅读杰克逊的文档时,我看到你应该能够在哪里包装列表,即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 选项。

课程将遵循以下原则:

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); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JsonMappingException:当前令牌不是 START_OBJECT(需要解开根名称“Transaction[]”),而是 START_ARRAY 的相关文章

随机推荐

  • 如何在 SeekBar 上显示最大值和最小值?

    我正在尝试做的事情 我想实施一个SeekBar在 Android 应用程序中SeekBar我还想显示最大值和最小值 最小值始终为 0 但最大值取决于剪辑长度 例如 0 180 有没有办法显示用户移动时选择的值 在搜索栏本身上 SeekBar
  • Visual Studio Code 更新后,HTML 文件中的智能 Javascript 建议不再起作用

    我使用 Visual Studio Code 已有几个月了 我已经习惯了里面的聪明建议
  • 为什么Java程序需要“main()”方法?

    这只是一个命名约定 为什么从 shell 执行程序时不能调用任何方法 例如 gt java myPackage MyClass myOwnEntryPoint String str 是的 这是一个命名约定 继承自C 这样做的好处是 只需查看
  • 为 Seq2Seq 模型添加注意力层

    我已经构建了编码器 解码器的 Seq2Seq 模型 我想为其添加一个注意力层 我尝试添加注意力层通过这个但这没有帮助 这是我最初的代码 没有注意 Encoder encoder inputs Input shape None enc emb
  • LESS 使用类名声明变量?

    我有一个为元素设置填充等的按钮类 后面跟着一个定义背景颜色的类 button padding 0 5em 1em text transform uppercase color fff green background green decla
  • Rails3 与 SQL Server 2000 对话-----ActiveRecord::JDBCError: 'ROW_NUMBER' 不是可识别的函数名称

    一个简单的 Rails 3 应用程序尝试使用 activerecord jdbc adapter 与 SQL Server 2000 进行通信 我尝试了 microsoft jdbc 驱动程序和 jtds 驱动程序 似乎连接数据库正常 当需
  • 如何查看X++ select语句的SQL表达式?

    我在 AX 的一份工作中有以下声明 select RecId from pjiTable join pID Type PrId from sjTable where pjiTable Prid sjTable PrId sjTable Ty
  • Ember、mixin 来检测视图/组件之外的点击

    我正在编写一个 Mixin 来处理用户在视图 组件之外单击时的情况 这是混合 App ClickElsewhereMixin Ember Mixin create onClickElsewhere Ember K didRender fun
  • 重复 cookie 查询或存储在视图状态中?哪个是更好的做法?

    我有一个用户登录的内部网站 该数据被保存为 cookie 从那里 用户继续他们的快乐之路 应用程序经常会查询身份验证记录以确定用户拥有哪些权限 我的问题是 在需要时查询 cookie 来获取用户数据 还是将用户信息保存在 viewstate
  • 使用 SearchView 实现自定义搜索

    我想在我的应用程序中实现搜索 但我不想使用单独的活动来显示我的搜索结果 相反 我只想使用显示在下方的建议列表SearchView 我可以用setOnQueryTextListener on the SearchView 聆听输入并搜索结果
  • Rails 4:heroku 基本计划上的 ConnectionTimeoutError

    我们正在研究一个简单的 Rails 4 网络应用程序 由 Heroku 托管 免费计划 最近 我们正在经历一些ActiveRecord ConnectionTimeoutError 每隔几天就会发生一次 我们并不真正了解其背后的原因 您可能
  • Laravel 5 路由分页 url 编码问题

    我构建了一个 Laravel 5 应用程序 现在正在测试它如何处理不同的输入 因此我遇到了一个奇怪的问题 在标题中我有一个搜索字段 它返回结果 按 10 分页 问题 如果用户输入一个字母 例如英语中的 e 一切都会正常 但是 当用户输入字母
  • 使用正则表达式重新字符串匹配提取 URL 链接 - Python

    我一直在尝试使用 re api 从文本文件中提取 URL 任何以 http https 和 www 开头的链接 该文件包含文本以及 html 源代码 html 部分很容易 因为我可以使用 BeautifulSoup 提取它们 但普通文本似乎
  • 工作窃取算法[关闭]

    Closed 这个问题需要多问focused 目前不接受答案 我正在读一篇关于并发运行时的文章 其中有一个算法名为work stealing在本文中 但我不知道这个算法是什么 所以我想要一些解释或一些好的链接来帮助我介绍这个算法 这些有帮助
  • R错误:要替换的项目数不是替换长度R的倍数

    我编写了以下代码并收到错误 要替换的项目数不是代码行中替换长度的倍数 X after count c censN1 censN2 censN3 网上查了一下 发现问题可能是由于预先确定的样本数量不匹配造成的 n samples of NA和
  • 如何通过不同端口(80 除外)启动 nginx

    你好 我是 nginx 的新手 我尝试在我的服务器 运行 Ubuntu 4 上设置它 该服务器已经运行了 apache 所以之后我apt get install它 我尝试启动nginx 然后我收到这样的消息 Starting nginx t
  • 致命错误和非致命错误之间的区别

    当尝试编译代码时 IDE 中显示的致命错误和非致命错误之间的主要区别是什么 在这两种情况下 编译器都会显示错误消息 并且程序不会被编译 致命错误是编译器或链接器中未定义的编译器错误吗 A 致命错误可能会立即停止编译该翻译单元 编译器可能会在
  • 请求实体太大 PHP

    在我的 CakePHP 站点之一中 我收到此错误 请求的实体太大 我不知道出了什么问题 我认为我通过表单发布的数据太大 我在搜索引擎中搜索了这个 发现我必须增加post max size 默认情况下我认为它设置为8M 但不知道如何增加pos
  • Puppeteer - 协议错误 (Page.navigate):目标已关闭

    正如您在下面的示例代码中看到的 我使用 Puppeteer 和 Node 中的一组工作人员来通过给定 URL 运行多个网站屏幕截图请求 const cluster require cluster const express require
  • JsonMappingException:当前令牌不是 START_OBJECT(需要解开根名称“Transaction[]”),而是 START_ARRAY

    我正在使用Spring的休息模板来消耗充电接口 使用 JSON 作为负载格式 它一直进行得很顺利 但是当我尝试获取对象数组时 它惨败了 例如 请考虑以下情况 Transaction transactions restTemplate get