【问题标题】:Convert a Map<String, String> to a POJO for nested property keys将 Map<String, String> 转换为嵌套属性键的 POJO
【发布时间】:2019-02-07 10:01:17
【问题描述】:

我需要将 Map 转换为 POJO。我参考了下面的链接,对于简单的键(employeeId,firstName,lastName)它工作正常。

对于关联(有线)密钥(department.departmentId,department.departmentName)它不起作用

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;
}

地图的键和值是字符串,我是从其他函数中获得的。会有多个嵌套的属性键,例如 department.departmentId 或 地址.addressId

【问题讨论】:

  • 运行时,在线程“main”java.lang.IllegalArgumentException 中出现此错误异常:无法识别的字段“department.departmentId”

标签: json jackson gson


【解决方案1】:

您不需要使用department.departmentIddepartment.departmentName。而不是这样做,您必须在您的Department.class 上调用第二个convertValue。在此之后,您可以将创建的Department 设置为您的Employee

主要

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;
    }

员工

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;
    }
}

部门

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;
    }
}

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2013-05-24
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多