'MapStruct - Cannot find implementation

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?



Solution 1:[1]

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>

Solution 2:[2]

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>

Solution 3:[3]

My issue was resolved when I run the given command on my project -

mvn clean install

And then I noticed implementation files being generated. I guess generating implementation required execution of maven command.

Solution 4:[4]

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:

  • No default constructor in CarDto
  • Property model does not exist in Car (there is only marca)
  • Property theModel does not exist in CarDto (there is only laMarca)

Solution 5:[5]

I was reproducing the exact same error because I forgot to add @Mapper to the mapper interface:

@Mapper // <-- missing
public interface MyMapper {

It's a trivial mistake but it is easy to miss

Solution 6:[6]

Try configuring the MapStruct Eclipse Plugin in the eclipse IDE to resolve the issue.

Solution 7:[7]

It's proper to use @Mapper(componentModel = "spring")

over your Mapper class, so you will be able to insatiate in using injection, Like this:

@Autowired
private CarMapper carMapper;

for checking whether everything is ok or not, you can check your mapper implementation after compile. It should have @component on it. Like this

@Component
public class CarMapperImpl implements CarMapper {

Solution 8:[8]

I also faced similar issue when I was using lombok with mapstruct, Its a known issue. What worked for me was to add the lombok dependency in the annotationProcessorPaths.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
    <source>${java.version}</source>
    <target>${java.version}</target>
    <annotationProcessorPaths>
        <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${org.projectlombok.version}</version>
        </path>
        <!-- This is needed when using Lombok 1.18.16 and above -->
        <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
        </path>
        <!-- Mapstruct should follow the lombok path(s) -->
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </path>
    </annotationProcessorPaths>
</configuration>

please refer this answer for complete detail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 João Matos
Solution 2 Damian Szewc
Solution 3 Chetan Oswal
Solution 4 Filip
Solution 5 João Matos
Solution 6
Solution 7 João Dias
Solution 8 Yadav