Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导REST读取JSON数组有效负载_Java_Rest_Spring Boot - Fatal编程技术网

Java Spring引导REST读取JSON数组有效负载

Java Spring引导REST读取JSON数组有效负载,java,rest,spring-boot,Java,Rest,Spring Boot,我有这个邮戳方法 @PostMapping("/offreStage/{id}/users") public ResponseEntity<?> addAuthorizedStudents(@PathVariable Long id, @RequestBody Map<String, String> students) { return service.addAuthor

我有这个邮戳方法

@PostMapping("/offreStage/{id}/users")
public ResponseEntity<?> addAuthorizedStudents(@PathVariable Long id,
                                               @RequestBody Map<String, String> students) {
    return service.addAuthorizedStudentsToOffer(id, students);
}
这将返回以下内容:

“消息”:“JSON解析错误:无法反序列化的实例”
java.util.LinkedHashMap
out-of-START\u数组令牌;嵌套异常 is com.fasterxml.jackson.databind.exc.MismatchedInputException:无法 从START\u数组中反序列化
java.util.LinkedHashMap
的实例 [Source:(PushbackInputStream);行:1,列:1]处的\n标记“


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

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

  {
        "value": 15,
        "label": "student2@gmail.com"
  }
你需要一张地图列表,所以它不起作用。因此,函数中应该是:
List

或者更好地使用集合()

映射用于键值对,您有键值对列表


Map
更改为
List

由于发送JSON的方式,它无法工作。在您的示例中,实际上是将一组映射作为Json发送,并期望Spring将其转换为映射。在JS中,将结构转换为单个映射,或者可以使用后端中的对象映射json中的数据,如下所示:

[
    {
        "value": 15,
        "label": "student2@gmail.com"
    },
    {
        "value": 14,
        "label": "student21@gmail.com"
    }
]
然后你可以使用你的控制器,比如:

@PostMapping("/offreStage/{id}/users")
public ResponseEntity<?> addAuthorizedStudents(@PathVariable Long id,
                                               @RequestBody List<ObjectClass> students) {
    return service.addAuthorizedStudentsToOffer(id, students);
}
@PostMapping("/offreStage/{id}/users")
public ResponseEntity<?> addAuthorizedStudents(@PathVariable Long id,
                                               @RequestBody List<ObjectClass> students) {
    return service.addAuthorizedStudentsToOffer(id, students);
}
public class ObjectClass {
String value;
String label;
//getters and setters

}