How to map different Java Bean structures to each other

In our project, we need to map one nested beans structure to another. (In fact, JAXB displays Java representations of XML documents, which, for example, represent an incoming order document.) This should be mapped onto a completely different structure of the order document of another system.

What are the options for this? I would prefer something that meets the following requirements:

  • The display should warn me when a single field display is not defined
  • The mapping should have some default values, such as matching fields of the same name with each other and providing standard mappings, for example, for int for String and vice versa.
  • The display must be bidirectional.
  • When defining a mapping, you must be able to use the code.

A promising foundation for this is Dozer , but it does not match 1 and 4. The same with JBeanMapper . Just program it in Java 4, but not other requirements; using XSLT accomplishes perhaps 2, but nothing more. Do you have any ideas?

+6
java mapping javabeans
source share
7 answers

ModelMapper is one library that meets all your criteria. It offers a mapping API that uses the actual code to map properties, so you get code completion. And it offers validation to ensure that all destination properties are displayed. In addition, it offers some things that you were not even aware of, such as smart matching :)

Go to the ModelMapper homepage for more information:

http://modelmapper.org

+5
source share

Another alternative is MapStruct , which generates display code at build time, which leads to type mappings that do not require any dependencies during runtime (Disclaimer: I am the author of MapStruct).

+2
source share

When we had this problem, we finished mapping the fields in Java in the utility class. This is a real problem, especially when you have to map several different web services and have to record mappings for each of them (some of them are a simple 2D map of named attributes, not a hierarchy of objects, screams).

However, this way you have the opportunity to analyze the requirements of the target map to get the best display quality, set default values ​​when data is not installed, and so on. You can throw the usual "UndefinedMappingException" in your mapper where you need it. And, not being a fluffy library that uses reflections and / or complex XML mapping configuration files, it draws faster.

Ie, this is a post claiming to "write it in Java."

+1
source share

I solved a similar problem with commons-beanutils .

My ultimate goal is a large Java file that would build a data structure, and mapper created this file for me. Thus, I could create test data from the pictures taken while the program was running.

The mapper allowed me to define keys for sorting objects, a field to use to create object names in a Java file, and I used a map with the "class: field" field as the key. The value was an object that implements the simple "Mapping" interface (one method: toJava (object instance, String field, Object value)).

My main problem was compiling 2MB + Java files :)

+1
source share

Transmorph, EZMorph, Dozer, OTOM are some of them that you can look at to display Bean to Bean. I have used Dozer in the past and feel that it has become stable over the years.

+1
source share

Take a look at GeDA, which uses byte code to create runtime matching classes, making it extremely fast at http://genericdtoassembler.org/ . It will cover paragraph 1-3. Regarding point 4, I would suggest that it is an IDE, so I don’t think you will find any library that would facilitate this if it does not have IDE plugins.

+1
source share

There is an interesting solution that can help you if you map data structures that have a lot of slightly different options. For example, if you model business order structures for different business cases and product options, and for different options, different combinations of the same attributes as addresses, account identifiers, configuration information for products, etc. are required.

Thus, you have many structurally similar beans that contain different subsets of a number of attributes. If you do not want to write code for each individual variant, you can enter the Java interface with all the attributes that are found in these beans and use java.lang.reflect.Proxy to create proxy instances for the bean that you want to map and which returns null for getters, the actual bean does not have / throws an exception if a non-existing setter is called the actual bean. In terms of language, you introduce an interface into those beans, which has more methods than the bean itself.

0
source share

All Articles