249

I am trying to convert DO to DTO using java and looking for automated tool before start writing my own. I just wanted to know if there any free tool available for the same.

ravinikam
  • 3,666
  • 6
  • 28
  • 25
  • 1
    Jackson (the JSON library) has a method `new ObjectMapper().convertValue(...)`. Jackson also has support for annotations and annotation-mixins so I find it a very good option. – Simon Forsberg Aug 03 '17 at 13:24
  • 2
    Here is an article with different frameworks and performance comparison. https://www.baeldung.com/java-performance-mapping-frameworks – Christian Nov 05 '18 at 13:03
  • I readed all answers from this article, and decided to use [`MapStruct`](https://github.com/mapstruct/mapstruct) :) – wonsuc Feb 15 '22 at 01:40

9 Answers9

412

There are some libraries around there:

  • Commons-BeanUtils: ConvertUtils -> Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.

  • Commons-Lang: ArrayUtils -> Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).

  • Spring framework: Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings.

  • Dozer: Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

  • ModelMapper: ModelMapper is an intelligent object mapping framework that automatically maps objects to each other. It uses a convention based approach to map objects while providing a simple refactoring safe API for handling specific use cases.

  • MapStruct: MapStruct is a compile-time code generator for bean mappings, resulting in fast (no usage of reflection or similar), dependency-less and type-safe mapping code at runtime.

  • Orika: Orika uses byte code generation to create fast mappers with minimal overhead.

  • Selma: Compile-time code-generator for mappings

  • JMapper: Bean mapper generation using Annotation, XML or API (seems dead, last updated 2 years ago)

  • Smooks: The Smooks JavaBean Cartridge allows you to create and populate Java objects from your message data (i.e. bind data to) (suggested by superfilin in comments). (No longer under active development)

  • Commons-Convert: Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. The first stage will focus on Object to String and String to Object conversions. (seems dead, last update 2010)

  • Transmorph: Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized). (seems dead, last update 2013)

  • EZMorph: EZMorph is simple java library for transforming an Object to another Object. It supports transformations for primitives and Objects, for multidimensional arrays and transformations with DynaBeans (seems dead, last updated 2008)

  • Morph: Morph is a Java framework that eases the internal interoperability of an application. As information flows through an application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations. (seems dead, last update 2008)

  • Lorentz: Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one type into an object of another type. (seems dead)

  • OTOM: With OTOM, you can copy any data from any object to any other object. The possibilities are endless. Welcome to "Autumn". (seems dead)

Sled
  • 18,541
  • 27
  • 119
  • 168
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • none of the list supports annotation :( – ravinikam Sep 17 '09 at 07:21
  • 1
    Commons-Convert seems not just dormant but completely gone now ... – Jim Ferrans Mar 30 '10 at 02:53
  • Actually, if you want to add Moo to your list, then I can remove my answer, since your answer is clearly far more complete. ;) – Geoffrey Wiseman Apr 21 '11 at 15:37
  • @RN. Dozer supports annotation since version 5.3.2 (it didn't when you wrote your comment, but nice to know for people who are looking up this answer). – Mark Nov 09 '11 at 09:42
  • One more options is http://jtransfo.org/. this allows you to define the mapping using annotations on the TO. It has the additional feature of allow optional conversion of fields based on tags which are passed when doing the conversion. – Joachim Van der Auwera Jul 08 '13 at 09:40
  • Thanks. But it would be even more useful if this answer was split into multiple single-framework answers. In that case votes would show their actual popularity. – Vadzim Jan 30 '14 at 14:48
  • 3
    Another option for Java 8+: http://erchu.github.io/bean-cp/ (disclaimer: I'm the author of this project) – Rafal Nov 03 '14 at 07:54
  • Wouldn't consider Smooks since it's itroducing more problems. As i can see it's not developing, also it has no support of 3rd party frameworks (like JodaTime). Exception handling is awfull and doesn't provide meaningfull explanations. It's almost impossible to find even simple solutions for your problems (looks like developer don't use it much) – Ruslan May 01 '15 at 20:38
  • Another one to add to the list https://github.com/amgohan/zebra/ : DIY objects mapping and use zebra for unified way to inject mappers. for the sake of performance, you do objects mapping manually and don't know how organize your mappers. Zebra come with a simple and unified way to : - create your mappers - manage one way and reverse mapping in one class - register your mappers and reuse them anywhere in your application - manage deep mapping – amgohan May 29 '15 at 12:54
  • Another option is jTransfo (http://jtransfo.org). jTransfo uses annotations on the DTO objects. It has good integration with Spring Framework, CDI and Hibernate. It also has some specific features which can help for security. See also: https://www.parleys.com/tutorial/quickie-jtransfo-converting-between-transfer-domain-objects – Joachim Van der Auwera Sep 24 '15 at 19:44
  • 1
    @TmTron: Done and I edited the answer to remove the strike and reference the repo URL (since the main page still references 0.7.5) – patric.schenke Jun 19 '17 at 08:33
  • You can also use Bull https://github.com/HotelsDotCom/bull – Liquidpie Feb 06 '20 at 05:47
109

You could try Dozer.

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 29
    A very very long time after, I come to make an update, I hope my comment will be read : Now Dozer seems to be a dead, and a good option is to use MapStruct, it's easy (support annotations), fast (no reflection) and seems secure (at the moment). This is included in the @Pascal Thivent list although, but I advise it (even if I had some problems by using it along with lombok). – Alex Dec 17 '15 at 13:58
  • 3
    Dozer seems revived with version 6: https://mvnrepository.com/artifact/com.github.dozermapper/dozer-core – tkruse Jan 17 '18 at 02:34
  • [The project is currently not active and will more than likely be deprecated in the future. If you are looking to use Dozer on a greenfield project, we would discourage that. If you have been using Dozer for a while, we would suggest you start to think about migrating onto another library, such as: mapstruct, modelmapper](https://github.com/DozerMapper/dozer) – wonsuc Feb 15 '22 at 01:35
32

Another one is Orika - https://github.com/orika-mapper/orika

Orika is a Java Bean mapping framework that recursively copies (among other capabilities) data from one object to another. It can be very useful when developing multi-layered applications.

Orika focuses on automating as much as possible, while providing customization through configuration and extension where needed.

Orika enables the developer to :

  • Map complex and deeply structured objects
  • "Flatten" or "Expand" objects by mapping nested properties to top-level properties, and vice versa
  • Create mappers on-the-fly, and apply customizations to control some or all of the mapping
  • Create converters for complete control over the mapping of a specific set of objects anywhere in the object graph--by type, or even by specific property name
  • Handle proxies or enhanced objects (like those of Hibernate, or the various mock frameworks)
  • Apply bi-directional mapping with one configuration
  • Map to instances of an appropriate concrete class for a target abstract class or interface
  • Handle reverse mappings
  • Handle complex conventions beyond JavaBean specs.

Orika uses byte code generation to create fast mappers with minimal overhead.

Community
  • 1
  • 1
Tomas
  • 329
  • 3
  • 2
24

My ModelMapper is another library worth checking out. ModelMapper's design is different from other libraries in that it:

  • Automatically maps object models by intelligently matching source and destination properties
  • Provides a refactoring safe mapping API that uses actual code to map fields and methods rather than using strings
  • Utilizes convention based configuration for simple handling of custom scenarios

Check out the ModelMapper site for more info:

http://modelmapper.org

Makyen
  • 31,849
  • 12
  • 86
  • 121
Jonathan
  • 5,027
  • 39
  • 48
14

Use Apache commons beanutils:

static void copyProperties(Object dest, Object orig) -Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

http://commons.apache.org/proper/commons-beanutils/

Harald Brabenetz
  • 445
  • 4
  • 12
Pablojim
  • 8,542
  • 8
  • 45
  • 69
  • 3
    yeh,but properties that are in the source bean, but do not in the destination bean are simply ignored. Thanks anyways Pablojim :) I think I have to go with Dozer since we have some property names different for DO and DTO :( – ravinikam Sep 16 '09 at 13:30
  • 1
    Fair enough - I didn't know this... Pascal's list was pretty impressive! – Pablojim Sep 16 '09 at 18:33
6

There is one more Java mapping engine/framework Nomin: http://nomin.sourceforge.net.

Dmitry
  • 332
  • 3
  • 8
3

I suggest you try JMapper Framework.

It is a Java bean to Java bean mapper, allows you to perform the passage of data dynamically with annotations and / or XML.

With JMapper you can:

  • Create and enrich target objects
  • Apply a specific logic to the mapping
  • Automatically manage the XML file
  • Implement the 1 to N and N to 1 relationships
  • Implement explicit conversions
  • Apply inherited configurations
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
Alessandro
  • 282
  • 3
  • 10
2

I'm happy to add Moo as an option, although clearly I'm biased towards it: http://geoffreywiseman.github.com/Moo/

It's very easy to use for simple cases, reasonable capable for more complex cases, although there are still some areas where I can imagine enhancing it for even further complexities.

Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
2

You can also try mapping framework based on Dozer, but with Excel mapping declaration. They've got some tools and additional cool features. Check at http://openl-tablets.sf.net/mapper

Ivan
  • 21
  • 1