dozer
Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another.
Top Related Projects
Intelligent object mapping
An annotation processor for generating type-safe bean mappers
Simpler, better and faster Java bean mapping framework
Quick Overview
Dozer is a Java-based mapping framework that recursively copies data from one object to another. It efficiently transfers data between Java beans, complex types, and hierarchical structures, eliminating the need for manual data copying code.
Pros
- Simplifies object-to-object mapping with minimal configuration
- Supports deep mapping of nested objects and collections
- Offers both XML and API-based configuration options
- Provides custom converters for complex mapping scenarios
Cons
- Performance overhead compared to manual mapping for simple use cases
- Learning curve for advanced mapping configurations
- Limited support for mapping to/from non-Java objects
- Potential for hidden bugs due to automatic type conversions
Code Examples
- Basic mapping between two classes:
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
SourceClass source = new SourceClass("John", 30);
DestinationClass dest = mapper.map(source, DestinationClass.class);
- Custom field mapping using API configuration:
Mapper mapper = DozerBeanMapperBuilder.create()
.withMappingBuilder(new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(SourceClass.class, DestinationClass.class)
.fields("sourceField", "destField");
}
})
.build();
- Using custom converters:
public class DateConverter implements CustomConverter<String, Date> {
@Override
public Date convert(String source, Date destination, Class<?> destClass, Class<?> sourceClass) {
return new SimpleDateFormat("yyyy-MM-dd").parse(source);
}
}
Mapper mapper = DozerBeanMapperBuilder.create()
.withCustomConverter(new DateConverter())
.build();
Getting Started
To use Dozer in your project, add the following dependency to your Maven pom.xml
:
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>6.5.2</version>
</dependency>
Create a basic mapping:
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
DestinationObject dest = mapper.map(sourceObject, DestinationObject.class);
For more complex mappings, create a dozer-mapping.xml
file in your classpath:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
<mapping>
<class-a>com.example.SourceClass</class-a>
<class-b>com.example.DestinationClass</class-b>
<field>
<a>sourceField</a>
<b>destField</b>
</field>
</mapping>
</mappings>
Then load the mapping file:
Mapper mapper = DozerBeanMapperBuilder.create()
.withMappingFiles("dozer-mapping.xml")
.build();
Competitor Comparisons
Intelligent object mapping
Pros of ModelMapper
- Faster mapping performance, especially for large datasets
- More intuitive API with less configuration required
- Better support for complex object graphs and nested mappings
Cons of ModelMapper
- Less extensive documentation compared to Dozer
- Fewer built-in converters for handling specific data types
- May require more manual configuration for very complex mappings
Code Comparison
ModelMapper:
ModelMapper modelMapper = new ModelMapper();
DestinationType dest = modelMapper.map(source, DestinationType.class);
Dozer:
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
DestinationType dest = mapper.map(source, DestinationType.class);
Both libraries offer similar basic usage, but ModelMapper often requires less configuration for complex mappings. Dozer may need more explicit mapping definitions in its XML configuration or through its API for intricate scenarios.
ModelMapper excels in performance and ease of use for most common mapping scenarios, while Dozer provides more fine-grained control and extensive documentation. The choice between the two depends on the specific requirements of your project, such as performance needs, complexity of mappings, and desired level of customization.
An annotation processor for generating type-safe bean mappers
Pros of MapStruct
- Compile-time code generation, resulting in faster execution and earlier error detection
- Type-safe mapping with minimal runtime overhead
- Extensive IDE support for code completion and refactoring
Cons of MapStruct
- Steeper learning curve due to annotation-based configuration
- Less flexibility for complex mappings compared to runtime-based solutions
Code Comparison
MapStruct:
@Mapper
public interface CarMapper {
@Mapping(source = "numberOfSeats", target = "seatCount")
CarDto carToCarDto(Car car);
}
Dozer:
<mapping>
<class-a>com.example.Car</class-a>
<class-b>com.example.CarDto</class-b>
<field>
<a>numberOfSeats</a>
<b>seatCount</b>
</field>
</mapping>
MapStruct uses Java annotations for mapping configuration, while Dozer typically uses XML or API-based configuration. MapStruct generates mapping code at compile-time, whereas Dozer performs mappings at runtime using reflection.
Both libraries aim to simplify object mapping in Java applications, but they differ in their approach and performance characteristics. MapStruct is generally faster and provides stronger type safety, while Dozer offers more flexibility for complex mapping scenarios.
Simpler, better and faster Java bean mapping framework
Pros of Orika
- Generally faster performance, especially for large datasets
- More flexible API with support for custom mappers and converters
- Better support for generic types and collections
Cons of Orika
- Steeper learning curve due to more complex API
- Less extensive documentation compared to Dozer
- Fewer built-in type conversions out of the box
Code Comparison
Dozer:
DozerBeanMapper mapper = new DozerBeanMapper();
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);
Orika:
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(SourceClass.class, DestinationClass.class).byDefault().register();
MapperFacade mapper = mapperFactory.getMapperFacade();
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);
Both Dozer and Orika are popular Java bean mapping libraries, each with its own strengths. Dozer is known for its simplicity and ease of use, making it a good choice for straightforward mapping scenarios. Orika, on the other hand, offers better performance and more advanced features, making it suitable for complex mapping requirements and high-performance applications. The choice between the two depends on the specific needs of your project, considering factors such as performance requirements, complexity of mappings, and team expertise.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Dozer
Project Activity
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:
For those moving to mapstruct, the community has created a Intellij plugin that can help with the migration.
Why Map?
A mapping framework is useful in a layered architecture where you are creating layers of abstraction by encapsulating changes to particular data objects vs. propagating these objects to other layers (i.e. external service data objects, domain objects, data transfer objects, internal service data objects).
Mapping between data objects has traditionally been addressed by hand coding value object assemblers (or converters) that copy data between the objects. Most programmers will develop some sort of custom mapping framework and spend countless hours and thousands of lines of code mapping to and from their different data object.
This type of code for such conversions is rather boring to write, so why not do it automatically?
What is Dozer?
Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another, it is an open source mapping framework that is robust, generic, flexible, reusable, and configurable.
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.
Dozer not only supports mapping between attribute names, but also automatically converting between types. Most conversion scenarios are supported out of the box, but Dozer also allows you to specify custom conversions via XML or code-based configuration.
Getting Started
Check out the Getting Started Guide, Full User Guide or GitBook for advanced information.
Getting the Distribution
If you are using Maven, simply copy-paste this dependency to your project.
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>7.0.0</version>
</dependency>
Simple Example
<mapping>
<class-a>yourpackage.SourceClassName</class-a>
<class-b>yourpackage.DestinationClassName</class-b>
<field>
<a>yourSourceFieldName</a>
<b>yourDestinationFieldName</b>
</field>
</mapping>
SourceClassName sourceObject = new SourceClassName();
sourceObject.setYourSourceFieldName("Dozer");
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
DestinationClassName destObject = mapper.map(sourceObject, DestinationClassName.class);
assertTrue(destObject.getYourDestinationFieldName().equals(sourceObject.getYourSourceFieldName()));
Top Related Projects
Intelligent object mapping
An annotation processor for generating type-safe bean mappers
Simpler, better and faster Java bean mapping framework
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot