print the contents of list along with the sublist using spring mvc

jcrshankar :

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;

}
}
Kosonome :

Since your @RestController it's already returning a list of Category and this category already has inside a list of Courses, 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;
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329041&siteId=1