Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 Boot REST API中的对象数组_Java_Spring Boot_Rest - Fatal编程技术网

Java Spring Boot REST API中的对象数组

Java Spring Boot REST API中的对象数组,java,spring-boot,rest,Java,Spring Boot,Rest,[已解决!代码中反映的解决方案] 我正在从事一个使用Spring Boot的项目,我需要数组,其中包含JSON中的对象组。它应该如下示例所示: { "Spring20": [ { "CSCXXX": "Machine Learning", . . . }, { "CSCXXX": "Computer Vision",

[已解决!代码中反映的解决方案]

我正在从事一个使用Spring Boot的项目,我需要数组,其中包含JSON中的对象组。它应该如下示例所示:

{  
  "Spring20": [
    {
      "CSCXXX": "Machine Learning",
       . . . 
    },
    {
      "CSCXXX": "Computer Vision",
       . . . 
    }
  ],  

  "Fall20": [
    {
      "EEXXX": "Electronics",
       . . . 
    }
  ]
}
我尝试使用一个映射来存储数组主题名的字符串,并使用一个列表来保存所有类对象。它位于服务类中

 HashMap<String, List<Courses>> courseList = new HashMap<String, List<Courses>>();
我试图研究这个问题,但没有找到任何有用的链接或示例,人们可以使用SpringBoot对JSON进行分类和组织。如果有什么可以帮忙的,请在下面评论。

编辑 下面的解决方案帮助修复了这些错误。我希望这篇文章能帮助任何可能遇到同样问题的人

这是我写的另一篇文章,它使用了MongoDB的地图。

请看以下内容:

每当我向数组中添加单个对象时,它都会替换以前在数组中的所有对象。例如,“Spring 2020”数组中有两个对象。如果我使用addCourse()发出另一个POST请求,两个对象都会被替换

问题是您正在使用
map.put()
。使用此方法时,如果映射以前包含键的映射,则旧值将替换为指定值。因此,旧值将被删除

解决方案是获取并更新现有条目

public void add( Map<String, List<Courses>> course) {
    course.forEach((semester, list) -> {
        if(courseList.contains(semester)) {
            courseList.get(semester).addAll(list); // Note, list accepts duplicates
        } else {
            courseList.put(semester, list);
        }
    });
}

/edit/{semster}/{id}
期望类似于
edit/spring20/1
这里
spring20
是semster,而
1
是id。您没有为
semster
使用PathVariable,这是REST API非常常见的困难之一。JSON补丁之类的想法试图解决这个问题,但这只是偶尔使用(Spring没有内置支持)。是的,我没有找到任何在线资源来解决这个问题。我很高兴能得到帮助并找到解决办法!
    public void edit(String semester, String id, Courses courses) {
          // Get the Course list from the Map
          List<Courses> updatedCourse = course_list.get(semester);
          // Loop and find the matching object within the list
          for(int i=0; i<updatedCourse.size(); i++){
            if (updatedCourse.get(i).getID().equals(id)){
                // Once object found, update its values in the list 
                updatedCourse.get(i).set(course.getSubject())
                . . .
                // Finally, replace the list within the map
                course_list.replace(semester,  updatedCourse);
            }
        }
    }
    @RequestMapping(method=RequestMethod.POST, value="/add")
    public void addCourse(@RequestBody Map<String, List<Courses>> course) {
        service.addTest(course);
    }

    @RequestMapping(method=RequestMethod.PUT, value="/edit/{semster}/{id}")
    public void addCourse(@PathVariable("semester") String semester, @PathVariable("id") String id, @RequestBody Courses course) {
        service.editTest(semester, id, course);
    }
PUT: localhost:8080/edit/Spring20/
 >> "message": "Missing URI template variable 'semester' for method parameter of type String",
public void add( Map<String, List<Courses>> course) {
    course.forEach((semester, list) -> {
        if(courseList.contains(semester)) {
            courseList.get(semester).addAll(list); // Note, list accepts duplicates
        } else {
            courseList.put(semester, list);
        }
    });
}
@RequestMapping(method=RequestMethod.PUT, value="/edit/{semester}/{id}")
public void addCourse(@PathVariable("semester") String semester, @PathVariable("id") String id, @RequestBody Courses course) {
    service.editTest(semester, course);
}