Java Bean to Java Bean mapper that recursively copies data from one object to another
npx @tessl/cli install tessl/maven-com-github-dozermapper--dozer-core@6.5.0Dozer is a powerful Java Bean to Java Bean mapper that recursively copies data from one object to another. It provides automatic property mapping with extensive customization options for complex mapping scenarios, including deep object graphs, collections, and custom type conversions.
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>6.5.2</version>
</dependency>import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.DozerBeanMapper;
import com.github.dozermapper.core.MappingException;import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
// Create mapper instance (should be singleton)
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
// Simple mapping - creates new destination instance
PersonDto dto = mapper.map(person, PersonDto.class);
// Map to existing instance
PersonDto existingDto = new PersonDto();
mapper.map(person, existingDto);
// Map with custom configuration
Mapper customMapper = DozerBeanMapperBuilder.create()
.withMappingFiles("dozer-mappings.xml")
.withCustomConverter(new DateToStringConverter())
.build();Dozer core is built around several key components:
DozerBeanMapperBuilderPrimary mapping functionality for copying data between Java objects with automatic property matching and type conversion.
public interface Mapper {
<T> T map(Object source, Class<T> destinationClass) throws MappingException;
void map(Object source, Object destination) throws MappingException;
<T> T map(Object source, Class<T> destinationClass, String mapId) throws MappingException;
void map(Object source, Object destination, String mapId) throws MappingException;
MappingMetadata getMappingMetadata();
MapperModelContext getMapperModelContext();
}Fluent API for creating and configuring mapper instances with custom settings, converters, and mapping files.
public final class DozerBeanMapperBuilder {
public static DozerBeanMapperBuilder create();
public static Mapper buildDefault();
public DozerBeanMapperBuilder withMappingFiles(String... mappingFiles);
public DozerBeanMapperBuilder withCustomConverter(CustomConverter customConverter);
public DozerBeanMapperBuilder withEventListener(EventListener eventListener);
public Mapper build();
}Extensible converter framework for handling complex type transformations and custom mapping logic.
public interface CustomConverter {
Object convert(Object existingDestinationFieldValue, Object sourceFieldValue,
Class<?> destinationClass, Class<?> sourceClass);
}
public abstract class DozerConverter<A, B> implements ConfigurableCustomConverter {
public DozerConverter(Class<A> prototypeA, Class<B> prototypeB);
public abstract B convertTo(A source, B destination);
public abstract A convertFrom(B source, A destination);
}Fluent Java API for defining mappings programmatically without XML configuration files.
public abstract class BeanMappingBuilder {
public abstract void configure();
protected TypeMappingBuilder mapping(Class<?> typeA, Class<?> typeB, TypeMappingOption... options);
protected FieldDefinition field(String name);
protected TypeDefinition type(Class<?> type);
}Lifecycle hooks for intercepting and customizing mapping operations at various stages.
public interface EventListener {
void onMappingStarted(Event event);
void onPreWritingDestinationValue(Event event);
void onPostWritingDestinationValue(Event event);
void onMappingFinished(Event event);
}
public interface Event {
EventTypes getType();
ClassMap getClassMap();
FieldMap getFieldMap();
Object getSourceObject();
Object getDestinationObject();
Object getDestinationValue();
}Runtime access to mapping metadata and configuration for debugging and dynamic behavior.
public interface MappingMetadata {
List<ClassMappingMetadata> getClassMappings();
List<ClassMappingMetadata> getClassMappingsBySource(Class<?> sourceClass);
List<ClassMappingMetadata> getClassMappingsByDestination(Class<?> destinationClass);
ClassMappingMetadata getClassMapping(Class<?> sourceClass, Class<?> destinationClass);
}Custom object creation during mapping for advanced instantiation patterns and dependency injection integration.
public interface BeanFactory {
Object createBean(Object source, Class<?> sourceClass, String targetBeanId, BeanContainer beanContainer);
}public class MappingException extends RuntimeException {
public MappingException(String message);
public MappingException(String message, Throwable cause);
public MappingException(Throwable cause);
}Dozer throws MappingException for all mapping-related errors including: