Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct mapper returns empty mapped object

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!

like image 227
jiangwensi Avatar asked Nov 05 '20 00:11

jiangwensi


2 Answers

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>
like image 173
Yassir Khaldi Avatar answered Oct 06 '22 13:10

Yassir Khaldi


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

link with similar issue: MapStruct and Lombok not working together

like image 31
Guram Kankava Avatar answered Oct 06 '22 15:10

Guram Kankava