Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mapstruct

> 编程语言
Open source

An annotation processor for generating type-safe bean mappers

7.7K stars0 likes2 views
WebsiteGitHub

About

An annotation processor for generating type-safe bean mappers

MapStruct - Java bean mappings, the easy way!

  • What is MapStruct?
  • Requirements
  • Using MapStruct
  • Maven
  • Gradle
  • Documentation and getting help
  • Building from Source
  • Links
  • Licensing

What is MapStruct?

MapStruct is a Java annotation 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:

java
@Mapper
public interface CarMapper {

    CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );

    @Mapping(target = "seatCount", source = "numberOfSeats")
    CarDto carToCarDto(Car car);
}

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.

Requirements

MapStruct requires Java 1.8 or later.

Using MapStruct

MapStruct works in command line builds (plain javac, via Maven, Gradle, Ant, etc.) and IDEs.

For Eclipse, a dedicated plug-in is in development (see https://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 (see https://plugins.jetbrains.com/plugin/10036-mapstruct-support).

Maven

For Maven-based projects, add the following to your POM file in order to use MapStruct (the dependencies are available at Maven Central):

…

Gradle

For Gradle, you need something along the following lines:

groovy
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 from Releases page.

Documentation and getting help

To learn more about MapStruct, refer to the project homepage. The reference documentation covers all provided functionality in detail. If you need help please ask it in the Discussions.

Building from Source

MapStruct uses Maven for its build. Java 21 is required for building MapStruct from source. To build the complete project, run

./mvnw clean install

from the root of the project directory. To skip the distribution module, run

./mvnw clean install -DskipDistribution=true

Importing into IDE

MapStruct 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.

IntelliJ

Make sure that you have at least IntelliJ 2018.2.x (needed since support for annotationProcessors from the maven-compiler-plugin is from that version). Enable annotation processing in IntelliJ (Build, Execution, Deployment -> Compiler -> Annotation Processors)

Eclipse

Make sure that you have the m2e_apt plugin installed.

Links

  • Homepage
  • Source code
  • Downloads
  • Issue tracker
  • User group
  • CI build

Licensing

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 at https://www.apache.org/licenses/LICENSE-2.0.

Issues· 544 open

View all issuesOpen on GitHub
  • #4076

    JSpecify: Propagate nullability annotations to mapper implementation

    bugJSpecifyUpdated Sep 19, 2026
  • #4132

    ProcessorTests are currently unable to reload Mapper implementation class

    bugtestUpdated Sep 19, 2026
  • #4034

    Cache `Type` instances in `TypeFactory.getType(...)` to avoid redundant work

    Updated Sep 17, 2026
  • #4128

    NullValuePropertyMappingStrategy.IGNORE strategy ignored for Optional fields

    bugUpdated Sep 16, 2026
  • #4123

    Optional source property that is itself null throws NullPointerException on the generated isPresent() check; NullValueCheckStrategy.ALWAYS is ignored

    Updated Sep 16, 2026
  • #3081

    NullValuePropertyMappingStrategy.IGNORE is ignored on final Lists

    bugUpdated Sep 10, 2026
  • #4118

    Collections no longer copied if marked non-null

    bugJSpecifyUpdated Aug 27, 2026
  • #4066

    Parallel source and target traversal with `@ParallelSource`

    Updated Aug 25, 2026
  • #4038

    Mapstruct mapping incorrect properties when one of them is supporting adder

    bugUpdated Aug 23, 2026
  • #3211

    Option to prevent fluent setter style

    featureUpdated Aug 20, 2026

> Tags

Javaannotation-processorbean-mappingjavajavabeans

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言