Montag, 10. März 2014

mapstruct - Java bean mappings

In the Java space there are a lot of tools for bean mappings, for example a common tool is Dozer. But the most tools I know, does a lot of magic, by using the Java reflection API, this is bad for debugging the bean mapping logic. Now there is a new and hot Java open source tool called  mapstruct. The project provides a simple way to map Java beans. By generating the mapping code from a Java interface with annotations. A simple mapping for a Car bean and a Engine bean  can be found in the code bellow.
Mapstruct also provides a simple way for own mappings, for this a mapper can be define by the uses attribute in the Mapper  annotation on the interface, the example use a mapper called PowerMapper. The implementation of this mapper  is shown in the code bellow.
How the generated bean mapper can be used is show in the following JUnit test.
More Details about mapstruct see the project documentation. I think mapstruct it is a very promising bean mapping tool, try it and leave some comments here.