【问题标题】:print the contents of list along with the sublist using spring mvc使用 spring mvc 打印列表的内容以及子列表
【发布时间】:2019-09-13 18:53:23
【问题描述】:

如何 打印列表的内容和子列表。?有人可以帮我完成服务类逻辑吗? 问题如下为网络游戏创建一个程序,如下所示。 主列表 - 类别 类别子列表 - 课程

Categories Sample Data: (Add first 3 categories from the netplay app)
 "id": 1001,
 "name": "Computing",
 "description": "network of remote servers"here

Course Sample Data:(Add first 3 courses on every category added from the netplay app)
id : 2001,
Category id: 1001,
name : "AWS"
duration : 180
miles :100

这是服务类如何实现这里的逻辑以将列表的内容与子列表一起返回

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

import org.springframework.stereotype.Service;

@Service
public class ContentService {

 private List<Category> categories=new ArrayList<>(Arrays.asList(new Category(1001, "Computing", "network of remote servers"));

//put your code here.
public List<Category> getAllContent() {
    return categories;

}
}

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    由于您的@RestController 已经返回了Category 的列表,并且该类别已经在Courses 的列表中,因此您需要在您的服务中填写您的类别。

    @Service
    public class ContentService {
      private List<Category> categories = new ArrayList<>();
    
      public List<Category> getAllContent() {
        List<Course> courses = new ArrayList<>();
        courses.add(new Course(1, "Coursename", 200, 200, 1001));
    
        List<Category> categories = new ArrayList<>();
        categories.add(new Category(1001, "Computing", "network of remote servers", courses));
        return categories;
      }
    }
    

    【讨论】:

      【解决方案2】:

      如果你想打印列表的内容和子列表,不要使用

      ArrayList 会变得复杂只需使用地图并将地图中的值作为键并将**子列表作为地图中的值**并打印

      【讨论】:

        【解决方案3】:
        public List<Category> getAllContent() {
        List<Course> courses = new ArrayList<>();
        courses.add(new Course(2001, "AWS Essentials",180, 100,1001));
            courses.add(new Course(2002, "java ",180, 100,1002));
                courses.add(new Course(2003, "C ",180, 100,1003));
        List<Category> categories = new ArrayList<>();
        categories.add(new Category(1001, "Cloud Computing", "network of remote servers hosted on the Internet to store", courses));
        categories.add(new Category(1002, "java", "oops", courses));
        categories.add(new Category(1003, "andriod", "mobile", courses));
        System.out.println("categoriessize::"+categories.size());
        return categories;
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-10
          • 1970-01-01
          相关资源
          最近更新 更多