I need to convert a Map to POJO. I referred the below link, for simple key(employeeId,firstName,lastName)it is working fine.
For a associated(wired)key(department.departmentId,department.departmentName) it is not working
Convert a Map<String, String> to a POJO
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Employee {
private int employeeId;
private String firstName;
private String lastName;
private Department department;
public static void main(String[] args) {
Map<String,String> input = constructMap();
final ObjectMapper mapper = new ObjectMapper();
//mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
final Employee employee = mapper.convertValue(input, Employee.class);
System.out.println(employee);
}
private static Map<String,String> constructMap() {
Map<String,String> obj = new HashMap<String,String>();
obj.put("employeeId","1");
obj.put("firstName","firstName");
obj.put("lastName","lastName");
//obj.put("department.departmentId","123");
//obj.put("department.departmentName","Physics");
return obj;
}
} // Employee class end
public class Department {
private int departmentId;
private String departmentName;
}
key and value of the map is string, which I am getting from some other function. There will be multiple nested property keys like department.departmentId or address.addressId