Dead-simple object mapping in Java
Java
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
mappify-api
mappify-handcraft-spring
mappify-handcraft
.gitignore
.travis.settings.xml
.travis.yml
pom.xml
readme.md

readme.md

mappify Build Status

Dead-simple object mapping in Java.

Usage

Standalone usage

  1. Add Maven dependency (or download and include jars from here)
<dependency>
    <groupId>com.github.shyiko.mappify</groupId>
    <artifactId>mappify-handcraft</artifactId>
    <version>1.3.2</version>
</dependency>

The latest development version always available through Sonatype Snapshots repository.

  1. Define mapping provider(s)
class MappingProvider {
    @Mapping
    public void mapFromSourceToTarget(Source source, Target target) {
        target.name = "Target #" + source.id;
    }
}

NOTE: Each mapping provider can define arbitrary number of @Mapping-annotated methods.

  1. Create & configure Mapper instance
Mapper mapper = new HandcraftMapper();
mapper.register(new MappingProvider()); // call this method for each mapping provider you have
  1. Use it across the application
Target mappedTarget = mapper.map(new Source(1), Target.class);
Set<Target> mappedTargets =
    mapper.mapToHashSet(Array.asList(new Source(2), new Source(3)), Target.class);

Integration with Spring

  1. Add Maven dependency
<dependency>
    <groupId>com.github.shyiko.mappify</groupId>
    <artifactId>mappify-handcraft-spring</artifactId>
    <version>1.3.1</version>
</dependency>
  1. Include the following line into your spring context configuration file
<import resource="classpath:mappify-handcraft-spring-context.xml"/>
  1. Define mapping provider(s)
@MappingProvider
public class EntityMappingProvider {

    @Mapping
    public void mapToDTO(Entity entity, EntityDTO dto) {
        ...
    }

    @Mapping
    public void mapFromDTO(EntityDTO entityDTO, Entity entity, MappingContext context) {
        ...
    }

    ...
}

NOTE: @MappingProvider extends @Controller. As a result, mapping providers are discoverable with Spring's <context:component-scan .../>. If you don't use it, just make sure mapping providers are wired into the context (e.g. by using <bean ... class="mapping provider class"/>).

  1. Call the Mapper
@Autowired
private Mapper mapper;
...
Entity entity = ...;
EntityDTO dto = mapper.map(entity, EntityDTO.class);
...

Testing with Mockito

Mapper mapper = mock(AbstractMapper.class, Mockito.CALLS_REAL_METHODS);
do...(...).when(mapper).map(any(), any(), anyString(), any(MappingContext.class));

Frequently Asked Question

  • Why does Mapper have mapToArrayList but no mapToLinkedList method?

    All mapTo... aliases were introduced as optimizations to the mapping process. So, instead of mapper.map(collection, Target.class, new ArrayList<Target>(collection.size())) one could simply use mapper.mapToArrayList(collection, Target.class). For some data types, like LinkedList, there is nothing that can be done to improve the performance. As a result, simple mapper.map(collection, Target.class, new LinkedList<Target>()) will do just fine.

License

Apache License, Version 2.0