» Assume we have simple REST API available at the endpoint https://dummy.restapiexample.com/api/v1/employee/1. We will use ObjectMapper and StdDeserializer to extract the JSON response.
Note: You can use the internal JSON Viewer tool here to perform simple manipulations on the JSON file.
» There are various methods and libraries for performing HTTP requests in Java here, including Apache HttpClient, Java 11's HttpClient, Spring's RestTemplate, and OkHttp.
[pom.xml]
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
[App.java]
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("https://dummy.restapiexample.com/api/v1/employee/1");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
// TODO
» Install the dependencies needed to use ObjectMapper.
[pom.xml]
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
[App.java]
...
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Map.class, new EmployeeDeserializer());
mapper.registerModule(module);
Map<String, Object> employeeData = mapper.readValue(result, Map.class);
// Print the extracted information
System.out.println("Employee Name: " + employeeData.get("employee_name"));
System.out.println("Employee Salary: " + employeeData.get("employee_salary"));
System.out.println("Employee Age: " + employeeData.get("employee_age"));
» Instead of using SimpleModule, avoid it and directly deserialize the response into a custom class that matches the structure of the JSON data. Below is the revised approach:
[Employee.java]
class Employee {
@JsonProperty("employee_name")
private String employeeName;
@JsonProperty("employee_salary")
private int employeeSalary;
@JsonProperty("employee_age")
private int employeeAge;
// Getters and Setters
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public int getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(int employeeSalary) {
this.employeeSalary = employeeSalary;
}
public int getEmployeeAge() {
return employeeAge;
}
public void setEmployeeAge(int employeeAge) {
this.employeeAge = employeeAge;
}
}
[ApiResponse.java]
class ApiResponse {
private String status;
private Employee data;
private String message;
// Getters and Setters
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Employee getData() {
return data;
}
public void setData(Employee data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
[App.java]
ObjectMapper mapper = new ObjectMapper();
ApiResponse apiResponse = mapper.readValue(result, ApiResponse.class);
Employee employee = apiResponse.getData();
System.out.println("Employee Name: " + employee.getEmployeeName());
System.out.println("Employee Salary: " + employee.getEmployeeSalary());
System.out.println("Employee Age: " + employee.getEmployeeAge());