使用 spring mvc 打印列表的内容和子列表 [英] print the contents of list along with the sublist using spring mvc
问题描述
如何将列表的内容与子列表一起打印.?有人可以帮助我如何完成服务类逻辑吗?问题在下面为网络创建一个程序,如下所示.主列表 - 类别类别子列表 - 课程
how to Print the contents of list along with the subList.? can someone help me how to complete service class logic? question was below Create a program for netplay as below. Main List - Categories Category sub list - Courses
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
这是服务类如何实现这里的逻辑以将列表的内容与子列表一起返回
This is service class how to implement the logic here to return the content of list along with sublist
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;
}
}
推荐答案
自从你的 @RestController
它已经返回了一个 Category
的列表并且这个类别已经包含在一个列表中Course
,您需要在您的服务中填写您的类别.
Since your @RestController
it's already returning a list of Category
and this category already has inside a list of Course
s, you need fill your category with courses inside your service.
@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;
}
}
这篇关于使用 spring mvc 打印列表的内容和子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!