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?
Although my scenario is not the same as yours, it did result in the same error - hence I am posting this answer to help others that made the same mistake as I did and end up here looking for a solution.
I was importing the maven dependency:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
But forgot to add the annotation processor path in the maven compiler plugin:
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
I added mapstruct processor dependency and it worked for me
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</dependency>
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