我有这个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 ofjava.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/