Using latest Springboot and MapStruct versions and building with Maven, I am trying to implement the "Start Here" example given in the official MapStruct site
My code is even simpler:
pom.xml
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
(...)
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
(...)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Car.java
public class Car {
private String model;
// Constructors, setters and getters...
}
CarDto.java
public class CarDto {
private String theModel;
// Constructors, setters and getters...
}
CarMapper.java interface
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
@Mapping(source = "model", target = "theModel")
CarDto carToCarDto(Car car);
}
Main application
@SpringBootApplication
public class MappertestApplication {
public static void main(String[] args) {
SpringApplication.run(MappertestApplication.class, args);
Car c = new Car("Volkswagen");
CarDto cdto = CarMapper.INSTANCE.carToCarDto(c);
}
}
All the code is in this public repo: https://github.com/pgbonino/mappertest
When running, I am getting this error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.gallelloit.mappertest.MappertestApplication.main(MappertestApplication.java:14)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.gallelloit.mappertest.CarMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61)
at com.gallelloit.mappertest.CarMapper.<clinit>(CarMapper.java:10)
... 1 more
Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.gallelloit.mappertest.CarMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58)
... 2 more
I found this issue in the official MapStruct project, which seems to describe the same issue. However, in that case some custom configuration was being performed (custom name of the implementation). In my case everything is left as default.
Any idea?
You will need to make sure that your IDE is properly configured to invoke the annotation processors. Have a look at the IDE Setup.
Looking at the project you provided the code should not even compile. The MapStruct processor will emit compilation errors due to:
CarDto
model
does not exist in Car
(there is only marca
)theModel
does not exist in CarDto
(there is only laMarca
)