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!
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>
I removed annotation Processor Paths tag and declared bot lombok and mapstruct as dependency.
link with similar issue: MapStruct and Lombok not working together
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With