1

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

user1346346
  • 195
  • 2
  • 16
  • As run, getting this error Exception in thread "main" java.lang.IllegalArgumentException: Unrecognized field "department.departmentId" – user1346346 Feb 07 '19 at 10:07

1 Answers1

1

You don´t need to use department.departmentId and department.departmentName. Instead of doing this, you have to call a second convertValue on your Department.class. After this you can set the created Department to your Employee.

Main

public static void main(String[] args)
{
    Map<String,Object> input = constructMap();
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Employee employee = mapper.convertValue(input, Employee.class);
    Department department = mapper.convertValue(input, Department.class);
    employee.setDepartment(department);
    System.out.println(employee);
}

    private static Map<String, Object> constructMap()
    {
        Map<String, Object> obj = new HashMap<>();
        obj.put("employeeId", "1");
        obj.put("firstName", "firstName");
        obj.put("lastName", "lastName");
        obj.put("departmentId", "123");
        obj.put("departmentName", "Physics");

        return obj;
    }

Employee

public class Employee
{
    private int employeeId;
    private String firstName;
    private String lastName;
    private Department department;

    public int getEmployeeId()
    {
        return employeeId;
    }

    public void setEmployeeId(int employeeId)
    {
        this.employeeId = employeeId;
    }

    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 Department getDepartment()
    {
        return department;
    }

    public void setDepartment(Department department)
    {
        this.department = department;
    }
}

Department

public class Department
{
    private int departmentId;
    private String departmentName;

    public int getDepartmentId()
    {
        return departmentId;
    }

    public void setDepartmentId(int departmentId)
    {
        this.departmentId = departmentId;
    }

    public String getDepartmentName()
    {
        return departmentName;
    }

    public void setDepartmentName(String departmentName)
    {
        this.departmentName = departmentName;
    }
}
kAliert
  • 768
  • 9
  • 21