Lets see an example where courses is a list of maps . For this example in Student class we make a list of Course objects like this So our body will look like this for this example
Body:
{
"firstName": "H",
"lastName": "s",
"programme": "CSE",
"email": "hs@example.com",
"courses": [
{
"name": "Java",
"score": "70"
},
{
"name": "C++",
"score": "70"
}
]
}
Now lets create a Student Class
package postreqexample2;
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<Courses> courses;
public Student(String firstName, String lastName, String programme, String email)
{
this.firstName=firstName;
this.lastName=lastName;
this.programme=programme;
this.email=email;
this.courses = new ArrayList<Courses>();
}
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<Courses> getCourses() {
return courses;
}
public void setCourses(List<Courses> courses) {
this.courses = courses;
}
public void addCourse(String name, String score)
{
courses.add(new Courses(name, score));
}
}
Lets create a Course class
package postreqexample2;
public class Courses {
public String name;
public String score;
public Courses(String name, String score)
{
this.name=name;
this.score=score;
}
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;
}
}
Fianally lets create a post request
package postreqexample2;
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 PostReqExample2{
@Test
public void postreq()
{
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
Student st = new Student("H","S","CSE","hs@example.com");
st.addCourse("Jave","70");
st.addCourse("C++","70");
Response resp = given().contentType(ContentType.JSON).log().body().body(st).post();
resp.prettyPrint();
}
}