Post data using rest assured. Method 4.If one field is a list of maps and each map furhter contains a list

If courses contains list of maps and each map object further contains a list of topic.So we have list of different map objects that are key value pairs like name ->java ,score ->70 etc and within those maps we have another list of topics. So our body looks like this

Body:
{
    "firstName": "H",
    "lastName": "S",
    "programme": "CSE",
    "email": "hs@example.com",
    "courses": [
        {
            "name": "Java",
            "score": "70",
            "topics": [
                "opps",
                "Genrics",
                "collections"
            ]
        },
        {
            "name": "C++",
            "score": "70",
            "topics": [
                "opps",
                "Genrics",
                "collections"
            ]
        }
    ]
}

So we make a arraylist of Course objects in Student class.

package postreqex3;

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

public class Student {
    
    
public String firstName;
public String lastName;
public String programme;
public String email;
public List<Course> courses;
    
public Student(String firstName,  String lastName, String programme, String email)
{
this.firstName=firstName;
this.lastName=lastName;
this.programme=programme;
this.email=email;
courses = new ArrayList<Course>();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getProgramme() {
return programme;
}
public void setProgramme(String programme) {
this.programme = programme;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

public List<Course> getCourses() {
return courses;
}
public void setCourse(List<Course> courses) {
this.courses = courses;
}

public Course addCourse(String name, String score)
{
Course c = new Course(name, score); 
courses.add(c);
return c;    }
}

Now lets create a course class. In this courses class we also define a list named topics. As each Course object now not only contains Strings but also a list of topics as well.

package postreqex3;

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

public class Course {
public String name;
public String score;
public List<String> topics;
public Course(String name, String score)
{
this.name=name;
this.score=score;
topics = new ArrayList<String>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public List<String> getTopics() {
return topics;
}
public void setTopics(List<String> topics) {
this.topics = topics;
}
public void addTopics(String topic)
{
topics.add(topic);
}
}

Now lets look into the post request

package postreqex3;

import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.testng.annotations.Test;

public class StudentPostReq {
@Test
public void postreq()
{
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
Student st = new Student("H","S","CSE","hs@example.com");
Course ob1 =st.addCourse("Java","70");
ob1.addTopics("opps");
ob1.addTopics("Genrics");
ob1.addTopics("collections");
Course ob2 =st.addCourse("C++","70");
ob2.addTopics("opps");
ob2.addTopics("Genrics");
ob2.addTopics("collections");
Response resp = given().contentType(ContentType.JSON).log().body().body(st).post();
resp.prettyPrint();
   }
}