Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thank you for replying to the question about converting Java objects from map #385

Open
flychao88 opened this issue Dec 23, 2021 · 0 comments

Comments

@flychao88
Copy link

Thank you very much for your reply

My requirement is that map nested objects are converted into Java objects, and there are nested objects in objects. In this case, they cannot be copied successfully.

Null is displayed

@test
public void testMapToBean() {
Map<String, Object> map = new HashMap();
map.put("name", "abcdef");

    Map<String, Object> subMap = new HashMap<>();
    subMap.put("subName", "aaaaaaaa");

    map.put("subUser11", subMap);

    final DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    mapperFactory.classMap(Map.class, UserA.class)
            .field("name", "name")
            .field("subUser11['subName']", "subUser.subName")
            .byDefault()
            .register();

    UserA userA = new UserA();
   

    mapperFactory.getMapperFacade(Map.class, UserA.class).map(map,userA );

   
    System.out.println("sub::::"+userA.getSubUser().getSubName());
}

public static class UserA {
private String name;
private SubUser subUser;

    public SubUser getSubUser() {
        return subUser;
    }

    public void setSubUser(SubUser subUser) {
        this.subUser = subUser;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public static class SubUser {
    public String subName;

    public String getSubName() {
        return subName;
    }

    public void setSubName(String subName) {
        this.subName = subName;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant