Skip to content

Support for Mapping from Map<String, ???> to Bean #1075

Closed

Description

@twothe

Refering to Stack-Overflow

The situation is: I have a REST service that must be called with a huge amount of parameters (as by customer request) and those should be stored in a database. The easiest way to do that would be to take the parameter map and map that to an entity object. As it is about the same type of code to map from an object to and object or from a map to an object I assume it should not be too difficult to allow mapping like this:

public MyEntity mapToEntity(final Map<String, String> parameters);

Essentially that should turn into code like this:

myEntity.setName( parameters.get("name") );
myEntity.setReferrer( stringToReferrer( parameters.get("referrer") ) );
//...

It would be nice to have other variable types supported as well, like Map<String, Object>, but I am not sure at which point it becomes too complicated.

Activity

  1. gunnarmorling commented on Feb 17, 2017

    @gunnarmorling
    Member

    Yes, I've been thinking about this, too. There also is #717 which is related. It'd require quite some changes I reckon but I'd be sympathetic to adding this. For Map<String, Object> we'd need a new property on @Mapping for (optionally) specifying the type of the property in the map.

  2. filiphr commented on Feb 18, 2017

    @filiphr
    Member

    @gunnarmorling I am not sure that we will need to do something special to support mapping from Map<String, Object> into a bean. If you look at the map as a bean that has a getter that returns an Object, then it is not different than what we have now.

    We already have a mechanism that allows us to map Object to B. The user will have to provide the mapping classes from Object to their respective types (something that needs to be done for String also). I am not sure how it'll work for the reverse type, but I think that it can be done in a similar way.

  3. Raild commented on Feb 21, 2017

    @Raild

    I've tried to do it in the following way:

    @Mapping(expression = "java(parameters.get(\"name\"))", target = "name")
    public MyEntity mapToEntity(final Map<String, String> parameters);

    But is it possible to convert the value if the MyEntity.name type is boolean (without calling Boolean.parse(...) manually)?

  4. added 10 commits that reference this issue on Mar 8, 2019
  5. added this to the 1.4.0 milestone on Sep 18, 2019
  6. 30 remaining items

  7. added a commit that references this issue on Dec 30, 2020
  8. added a commit that references this issue on Jan 8, 2021
  9. added 5 commits that reference this issue on Jan 16, 2021
  10. jishei-128 commented on Apr 13, 2021

    @jishei-128

    Any updates on this? We could really benefit from this in our current project :-)

  11. ckosmowski commented on Apr 13, 2021

    @ckosmowski
    Contributor

    Actually in #2317 this is basically implemented and working. However like most of us because of the limited time i am granted to spend on open source projects during work time. It seems like there are still some unit tests missing, but unfortunately i am out of sprint time for this (for now). Whould you like to take over? Or maybe @sjaakd and @filiphr have the chance to finish it (Documentation?) We're almost there :-)

  12. ordisilent commented on May 7, 2021

    @ordisilent

    You can seek to join the spring team, so that better compatible with Java, better promotion

  13. added a commit that references this issue on Jun 5, 2021
  14. added a commit that references this issue on Jun 26, 2021
  15. added a commit that references this issue on Jun 27, 2021
  16. changed the title [-]Please add a feature to map from java.util.map to an object[/-] [+]Support for Mapping from Map<String, ???> to Bean[/+] on Jun 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions