Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1k
An annotation processor for generating type-safe bean mappers
License
mapstruct/mapstruct
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- What is MapStruct?
- Requirements
- Using MapStruct
- Maven
- Gradle
- Documentation and getting help
- Building from Source
- Links
- Licensing
MapStruct is a Javaannotation processor designed to generate type-safe and high-performance mappers for Java bean classes, including support for Java 16+ records.By automating the creation of mappings, MapStruct eliminates the need for tedious and error-prone manual coding.The generator provides sensible defaults and built-in type conversions, allowing it to handle standard mappings effortlessly, while also offering flexibility for custom configurations or specialized mapping behaviors.With seamless integration into modern Java projects, MapStruct can map between conventional beans, records, and even complex hierarchies, making it an adaptable tool for diverse Java applications.
Compared to mapping frameworks working at runtime, MapStruct offers the following advantages:
- Fast execution by using plain method invocations instead of reflection
- Compile-time type safety. Only objects and attributes mapping to each other can be mapped, so there's no accidental mapping of an order entity into a customer DTO, etc.
- Self-contained code—no runtime dependencies
- Clear error reports at build time if:
- mappings are incomplete (not all target properties are mapped)
- mappings are incorrect (cannot find a proper mapping method or type conversion)
- Easily debuggable mapping code (or editable by hand—e.g. in case of a bug in the generator)
To create a mapping between two types, declare a mapper interface like this:
@MapperpublicinterfaceCarMapper {CarMapperINSTANCE =Mappers.getMapper(CarMapper.class );@Mapping(target ="seatCount",source ="numberOfSeats")CarDtocarToCarDto(Carcar);}
At compile time MapStruct will generate an implementation of this interface. The generated implementation uses plain Java method invocations for mapping between source and target objects, i.e. no reflection is involved. By default, properties are mapped if they have the same name in source and target, but you can control this and many other aspects using@Mapping and a handful of other annotations.
MapStruct requires Java 1.8 or later.
MapStruct works in command line builds (plain javac, via Maven, Gradle, Ant, etc.) and IDEs.
For Eclipse, a dedicated plug-in is in development (seehttps://github.com/mapstruct/mapstruct-eclipse). It goes beyond what's possible with an annotation processor, providing content assist for annotation attributes, quick fixes and more.
For IntelliJ the plug-in is available within the IntelliJ marketplace (seehttps://plugins.jetbrains.com/plugin/10036-mapstruct-support).
For Maven-based projects, add the following to your POM file in order to use MapStruct (the dependencies are available at Maven Central):
...<properties> <org.mapstruct.version>1.6.3</org.mapstruct.version></properties>...<dependencies> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency></dependencies>...<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>17</source> <target>17</target> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins></build>...
For Gradle, you need something along the following lines:
plugins {... id"com.diffplug.eclipse.apt" version"3.26.0"// Only for Eclipse}dependencies {... implementation'org.mapstruct:mapstruct:1.6.3' annotationProcessor'org.mapstruct:mapstruct-processor:1.6.3' testAnnotationProcessor'org.mapstruct:mapstruct-processor:1.6.3'// if you are using mapstruct in test code}...If you don't work with a dependency management tool, you can obtain a distribution bundle fromReleases page.
To learn more about MapStruct, refer to theproject homepage. Thereference documentation covers all provided functionality in detail. If you need help please ask it in theDiscussions.
MapStruct uses Maven for its build. Java 21 is required for building MapStruct from source.To build the complete project, run
./mvnw clean installfrom the root of the project directory. To skip the distribution module, run
./mvnw clean install -DskipDistribution=trueMapStruct uses the gem annotation processor to generate mapping gems for its own annotations.Therefore, for seamless integration within an IDE annotation processing needs to be enabled.
Make sure that you have at least IntelliJ 2018.2.x (needed since support forannotationProcessors from themaven-compiler-plugin is from that version).Enable annotation processing in IntelliJ (Build, Execution, Deployment -> Compiler -> Annotation Processors)
Make sure that you have them2e_apt plugin installed.
MapStruct is licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License athttps://www.apache.org/licenses/LICENSE-2.0.
About
An annotation processor for generating type-safe bean mappers
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.