MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.
The generated mapping code uses plain method invocations and thus is fast, type-safe and easy to understand.
Why?
Multi-layered applications often require to map between different object models (e.g. entities and DTOs). Writing such mapping code is a tedious and error-prone task. MapStruct aims at simplifying this work by automating it as much as possible.
In contrast to other mapping frameworks MapStruct generates bean mappings at compile-time which ensures a high performance, allows for fast developer feedback and thorough error checking.
How?
MapStruct is an annotation processor which is plugged into the Java compiler and can be used in command-line builds (Maven, Gradle etc.) as well as from within your preferred IDE.
MapStruct uses sensible defaults but steps out of your way when it comes to configuring or implementing special behavior.
Latest News
MapStruct 1.1.0.Final seen in the wild!
I’m more than thrilled to report that MapStruct 1.1 Final has been spotted in the wild!
We grew that puppy for almost one year since the announcement of MapStruct 1.0, so it was about time to let it go and put a final release into your hands.
Besides a plethora of bug fixes the 1.1 release adds many new features which should be very welcome to users of MapStruct 1.0:
Nested target properties
@ValueMapping annotation for enum mappings
@Named annotation for simple string based mapping qualifiers
Support for custom hasXyz() methods to check the presence of source properties instead of null checks
Extended support of java.text.NumberFormat for Number types to String mapping
It’s my pleasure to announce the second candidate release of MapStruct 1.1!
This release fixes several bugs discovered in the first CR but also adds some new built-in conversions around date/time types (e.g. from/to the java.sql.* types and between java.time.LocalDate and java.util.Date).
Normally, we wouldn’t add new functionality during the CR phase, but as these conversions have been contributed by community members (kudos to you!), we thought it’d be nice to put them into a release as soon as possible.
The following shows how map two objects using MapStruct.
Let's assume we have a class representing cars (e.g. a JPA entity) and an accompanying data transfer object (DTO).
Both types are rather similar, only the seat count attributes have different names and the type attribute is of a special enum type in the Car class but is a plain string in the DTO.
The @Mapper annotation 1 marks the interface as mapping interface and lets the MapStruct processor kick in during compilation.
The actual mapping method 2 expects the source object as parameter and returns the target object. Its name can be freely chosen.
For attributes with different names in source and target object, the @Mapping annotation can be used to configure the names.
Where required and possible a type conversion will be executed for attributes with different types in source and target, e.g. the type attribute will be converted from the enumeration type into a string.
Of course there can be multiple mapping methods in one interface, for all of which an implementation will be generated by MapStruct.
An instance of the interface implementation can be retrieved from the Mappers class. By convention, the interface declares a member INSTANCE3, providing clients access to the mapper implementation.
Using the mapper
Based on the mapper interface, clients can perform object mappings in a very easy and type-safe manner:
@Test
public void shouldMapCarToDto() {
//given
Car car = new Car( "Morris", 5, CarType.SEDAN );
//when
CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );
//then
assertThat( carDto ).isNotNull();
assertThat( carDto.getMake() ).isEqualTo( "Morris" );
assertThat( carDto.getSeatCount() ).isEqualTo( 5 );
assertThat( carDto.getType() ).isEqualTo( "SEDAN" );
}
Tell me more!
You like what you see? Then check out the reference documentation to learn how to get started with MapStruct and which advanced features there are. In case you need help or want to propose a new feature just drop by on the mapstruct-users group.
You want to contribute to the development of MapStruct? That's great, this page has all the information you need.