Below API
return list of employees.
API:
http://localhost:8080/api/v1/employees
Method:
GET
[
{
"id": 1,
"firstName": "Deepak",
"lastName": "Moud"
},
{
"id": 2,
"firstName": "Srinivasa Rao",
"lastName": "Gumma"
},
{
"id": 3,
"firstName": "Purna Chandra",
"lastName": "Rao"
},
{
"id": 4,
"firstName": "Madhavi Latha",
"lastName": "Gumma"
},
{
"id": 5,
"firstName": "Raghava",
"lastName": "Reddy"
},
{
"id": 6,
"firstName": "Ram",
"lastName": "Gurram"
},
{
"id": 7,
"firstName": "Gopi",
"lastName": "Battu"
},
{
"id": 8,
"firstName": "Gopi",
"lastName": "Gumma"
},
{
"id": 9,
"firstName": "Radha",
"lastName": "Krishna"
}
]
Following
are the multiple ways to deserialize list of employees.
Example
1:
Response
response = given().header("Accept",
"application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().response();
Employee[]
empArray = response.as(Employee[].class);
List<Employee>
emps1 = Arrays.asList(empArray);
Example
2:
JsonNode
jsonNode = given().header("Accept", "application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().as(JsonNode.class);
ObjectMapper
mapper = new ObjectMapper();
List<Employee>
emps2 = mapper.convertValue(jsonNode, new TypeReference<List<Employee>>()
{
});
Example
3:
List<Employee>
emps3 = given().header("Accept",
"application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().body().jsonPath()
.getList(".",
Employee.class);
package com.sample.app.model;
public class Employee {
private int id;
private String firstName;
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
TestApp.java
package com.sample.app;
import static io.restassured.RestAssured.given;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class TestApp {
@Test
public void testApp() {
RestAssured.baseURI = "http://localhost:8080";
Response response = given().header("Accept", "application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().response();
Employee[] empArray = response.as(Employee[].class);
List<Employee> emps1 = Arrays.asList(empArray);
JsonNode jsonNode = given().header("Accept", "application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().as(JsonNode.class);
ObjectMapper mapper = new ObjectMapper();
List<Employee> emps2 = mapper.convertValue(jsonNode, new TypeReference<List<Employee>>() {
});
List<Employee> emps3 = given().header("Accept", "application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().body().jsonPath()
.getList(".", Employee.class);
emps1.forEach(System.out::println);
emps2.forEach(System.out::println);
emps3.forEach(System.out::println);
}
}
Run
TestApp.java as junit test, you will get below response.
Employee [id=1, firstName=Deepak, lastName=Moud] Employee [id=2, firstName=Srinivasa Rao, lastName=Gumma] Employee [id=3, firstName=Purna Chandra, lastName=Rao] Employee [id=4, firstName=Madhavi Latha, lastName=Gumma] Employee [id=5, firstName=Raghava, lastName=Reddy] Employee [id=6, firstName=Ram, lastName=Gurram] Employee [id=7, firstName=Gopi, lastName=Battu] Employee [id=8, firstName=Gopi, lastName=Gumma] Employee [id=9, firstName=Radha, lastName=Krishna] Employee [id=1, firstName=Deepak, lastName=Moud] Employee [id=2, firstName=Srinivasa Rao, lastName=Gumma] Employee [id=3, firstName=Purna Chandra, lastName=Rao] Employee [id=4, firstName=Madhavi Latha, lastName=Gumma] Employee [id=5, firstName=Raghava, lastName=Reddy] Employee [id=6, firstName=Ram, lastName=Gurram] Employee [id=7, firstName=Gopi, lastName=Battu] Employee [id=8, firstName=Gopi, lastName=Gumma] Employee [id=9, firstName=Radha, lastName=Krishna] Employee [id=1, firstName=Deepak, lastName=Moud] Employee [id=2, firstName=Srinivasa Rao, lastName=Gumma] Employee [id=3, firstName=Purna Chandra, lastName=Rao] Employee [id=4, firstName=Madhavi Latha, lastName=Gumma] Employee [id=5, firstName=Raghava, lastName=Reddy] Employee [id=6, firstName=Ram, lastName=Gurram] Employee [id=7, firstName=Gopi, lastName=Battu] Employee [id=8, firstName=Gopi, lastName=Gumma] Employee [id=9, firstName=Radha, lastName=Krishna]
No comments:
Post a Comment