将JSON数组发布到Spring Boot RestController

Orrymr

我发布这样的事情:

{
  "stuff": [
    {
      "thingId": 1,
      "thingNumber": "abc",
      "countryCode": "ZA"
    },
    {
      "thingId": 2,
      "thingNumber": "xyz",
      "countryCode": "US"
    }
  ]
}

我可以在控制器中检索数据,如下所示:

@RequestMapping(value = "/stuffs", method = RequestMethod.POST)
public String invoices(@RequestBody Map<String, List <Stuff>> stuffs) {

    return "Hooray";
}

我真正想做的是只将一个东西列表传递给控制器​​。即:

@RequestMapping(value = "/stuffs", method = RequestMethod.POST)
public String invoices(@RequestBody List <Stuff> stuffs) {

    return "I'm sad that this won't work.";
}

但是,杰克逊不喜欢这样。我得到的错误如下:

无法读取JSON文档:无法从[来源:java.io.PushbackInputStream@2a30a2f4; START_OBJECT令牌\ n中反序列化java.util.ArrayList实例。行:1,列:1];嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:无法在[来源:java.io.PushbackInputStream@2a30a2f4; 行:1,列:1]

我无法发送以下内容(因为它不是有效的JSON):

    {
      [
        {
          "thingId": 1,
          "thingNumber": "abc",
          "countryCode": "ZA"
        },
        {
          "thingId": 2,
          "thingNumber": "xyz",
          "countryCode": "US"
        }
      ]
    }

我确信这很简单,但是我无法从我的StackOverflow和Google搜索中获得答案。

任何帮助,将不胜感激!:)

阿米尔·卡拉布萨

您只需要发送不带花括号的邮件

   [
    {
      "thingId": 1,
      "thingNumber": "abc",
      "countryCode": "ZA"
    },
    {
      "thingId": 2,
      "thingNumber": "xyz",
      "countryCode": "US"
    }
  ]

在您的控制器中,当您告诉Jackson它将解析一个列表时,它应该是一个列表,而不是包含列表的对象

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

JSR-使用Spring Boot对Spring @RestController进行349 bean验证

为什么RestController在Spring Boot中不起作用?

RestController的设计难题-Spring Boot REST API

可以使用属性启用/禁用Spring Boot @RestController吗?

如何使用Spring Boot @RestController流式处理分块的响应

自动配置RestController Spring Boot

Spring-Boot Maven,缺少依赖项@RestController

Spring Boot RestController-PostMapping-将请求主体作为InputStream

使用restController发布对象

Spring Boot @restcontroller到@controller具有分页

Spring Boot RestController:拦截传入的请求

在Spring Boot RestController中从SecurityContextholder获取用户ID是个好主意吗?

Spring Boot RestController JSON序列化花费太多时间

RestController在oauth2 Spring Boot中不起作用

在Spring Boot中拦截对RestController的调用

Spring Boot-RestController反序列化YAML上传?

Spring Boot RestController不适用于lombok

Spring Boot RestController不返回JSON数据

有没有办法在Spring Boot RestController中清除“ consums = MediaType”?

没有.csrf()。disable()的Spring Boot RestController DELETE请求失败

从Spring Boot RestController使用多个服务时获取ClassCastException

使用方面时未调用 Spring Boot RestController 的构造函数

Spring Boot,Maven,在派生项目中禁用特定的RestController

Spring Boot @RestController 和 @Controller

Spring boot 中的 RestController-404 错误而不是 JSON 对象

未从 Spring Boot 应用程序调用多个 RestController

Spring Boot @RestController 无法自动将响应转换为 JSON

单元测试中的 Spring Boot @RestController @Autowired null

RestController 返回 404 (Spring Boot 2.3.7)