10

I'm trying to use MapStruct to map convert between dto and entity objects, however the generated mapper implementation only returns empty mapped object.

BeerMapperImpl

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2020-11-05T07:42:21+0800",
    comments = "version: 1.3.0.Final, compiler: javac, environment: Java 11 (Oracle Corporation)"
)
@Component
public class BeerMapperImpl implements BeerMapper {

    @Override
    public BeerDto beerToBeerDto(Beer beer) {
        if ( beer == null ) {
            return null;
        }

        BeerDto beerDto = new BeerDto();

        return beerDto;
    }
}

Below are my codes.

pom.xml

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.3.0.Final</version>
</dependency>

.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.3.0.Final</version>
            </path>
            <path>
                <groupId>
                    org.projectlombok
                </groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </path>
        </annotationProcessorPaths>
        <compilerArgs>
            <compilerArg>
                -Amapstruct.defaultComponentModel=spring
            </compilerArg>
        </compilerArgs>
    </configuration>
</plugin>

BeerMapper.java

@Mapper(uses = {DateMapper.class})
public interface BeerMapper {
    BeerDto beerToBeerDto(Beer beer);
    Beer beerDtoToBeer(Beer dto);
}

Anyone can help to advise what might go wrong in my codes? Thanks!

jiangwensi
  • 113
  • 2
  • 5

6 Answers6

33

I have found the solution without having to downgrade spring boot. The problem was that I was using lombok to generate getters and setters, on the other side mapstruct looks for getters and setters in your objects to populate them, but in this case it couldn't find them simply because during the compilation mapstruct was running before lombok could generate them.

The solution is simply to write the configuration of lombok before the configuration of mapstruct in your pom.xml file to execute lombok in first place. Make sure to run clean compile to delete old files.

Working configuration of maven-compiler-plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <annotationProcessorPaths>
      <!--Project Lombok compile preprocessor-->
      <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </path>
      <!--Maps Struct compile preprocessor-->
      <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct-ver}</version>
      </path>
    </annotationProcessorPaths>
  </configuration>
</plugin>
Yassir Khaldi
  • 1,452
  • 1
  • 17
  • 30
1

I removed annotation Processor Paths tag and declared bot lombok and mapstruct as dependency.

link with similar issue: MapStruct and Lombok not working together

Guram Kankava
  • 51
  • 1
  • 4
0

I resolved this issue after downgrading spring boot starter from 2.3.5.RELEASE to 2.1.5.RELEASE.

jiangwensi
  • 113
  • 2
  • 5
0

I've encountered a similar problem where all the values in the object created by the mapstruct mapping had null values when the original object was populated. When I checked the generated mapper in target the generated implementation did not contain an actual mapping. I found that this was easily fixed when I made a small change to the Mapper in my project and saved it. The mapper in target was re-generated with the mapping in place. I'm not sure why the mapper was generated incorrectly but this was a qick fix that worked for me.

Brad Jones
  • 91
  • 11
0

To the dto I added a @AllArgsConstructor, after removing it - works fine. Stupid, but works. The order of dependencies and paths was good, and spring boot version was older then proposed to downgrade

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 05:47
0

As official documentation suggest, newer versions of lombok require an aditional anotation processor dependency so you would need to add a new annotation processor like so (maven)

<path>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok-mapstruct-binding</artifactId>
  <version>***</version>
</path>

make sure you add the latest version compatible with your gradle build

for gradle users it will be something like this

annotationProcessor 'org.projectlombok:lombok-mapstruct-binding'

this way it will work with no problem in new versions of spring