java - Spring Boot REST 读取 JSON 数组有效负载

标签 java rest spring-boot

我有这个PostMapping方法

@PostMapping("/offreStage/{id}/users")
public ResponseEntity<?> addAuthorizedStudents(@PathVariable Long id,
                                               @RequestBody Map<String, String> students) {
    return service.addAuthorizedStudentsToOffer(id, students);
}

我使用以下 JSON 有效负载来发出我的发布请求:

[
    {
        "value": 15,
        "label": "student2@gmail.com"
    },
    {
        "value": 14,
        "label": "student21@gmail.com"
    }
]

这将返回以下内容:

"message": "JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",

最佳答案

发送的正文与函数中的正文不匹配。

更准确地说,这是您的 map :

  {
        "value": 15,
        "label": "student2@gmail.com"
  }

你需要一个 map 列表,所以它不起作用。所以应该是这样的:List<Map<String, String>>在函数中。 或者更好的是,使用集合( see this post )。

关于java - Spring Boot REST 读取 JSON 数组有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089072/

相关文章:

java - 仅在填写所有字段时使用 JAXB 将 JSON 转换为对象

java - postman REST 客户端原始 json 数据如何在 POST、PUT 调用中发送到服务器?

java - Spring JPA - 处理 DB/Repo 异常

java - 什么时候在 java 中使用 Enum 类?

java - Android R 无法解析为变量 - 更改包名称

java.sql.SQLException : Unknown type '246 in column 10 of 12 in binary-encoded re sult set 异常

python - 如何在 Django REST 框架中使用事务?

java - 尝试执行表单验证时出现错误

java - 自动/最适合 JTable 列,但拉伸(stretch)最后一列

java - 使用 spring 设置 jooq 数据源