简体   繁体   English

MapStruct - 找不到实现

[英]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使用最新的 Springboot 和 MapStruct 版本并使用 Maven 进行构建,我正在尝试实施官方 MapStruct 站点中给出的“从这里开始”示例

My code is even simpler:我的代码更简单:

pom.xml 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车.java

public class Car {

    private String model;

    // Constructors, setters and getters...

}

CarDto.java CarDto.java

public class CarDto {

    private String theModel;

    // Constructors, setters and getters...

} }

CarMapper.java interface CarMapper.java接口

@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所有代码都在这个公共仓库中: 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.我在官方的MapStruct项目中发现了这个问题,似乎描述的是同一个问题。 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.您需要确保正确配置 IDE 以调用注释处理器。 Have a look at the IDE Setup .查看IDE 设置

Looking at the project you provided the code should not even compile.查看您提供的代码的项目甚至不应该编译。 The MapStruct processor will emit compilation errors due to: MapStruct 处理器将发出编译错误,原因是:

  • No default constructor in CarDto CarDto中没有默认构造函数
  • Property model does not exist in Car (there is only marca )属性modelCar中不存在(只有marca
  • Property theModel does not exist in CarDto (there is only laMarca )属性theModelCarDto中不存在(只有laMarca

I added mapstruct processor dependency and it worked for me我添加了 mapstruct 处理器依赖项,它对我有用

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.3.1.Final</version>
</dependency>

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:我正在导入 maven 依赖项:

<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:但是忘记在maven编译插件中添加注解处理器路径:

    <annotationProcessorPaths>
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </path>
    </annotationProcessorPaths>

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.我猜想生成实现需要执行 maven 命令。

Try configuring the MapStruct Eclipse Plugin in the eclipse IDE to resolve the issue.尝试在 eclipse IDE 中配置MapStruct Eclipse 插件来解决此问题。

It's proper to use @Mapper(componentModel = "spring")使用@Mapper(componentModel = "spring")是正确的

over your Mapper class, so you will be able to insatiate in using injection, Like this:在您的映射器 class 上,因此您将能够对使用注射感到满意,就像这样:

@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.它应该有@component Like this像这样

@Component
public class CarMapperImpl implements CarMapper {

I was reproducing the exact same error because I forgot to add @Mapper to the mapper interface :我正在重现完全相同的错误,因为我忘记将 @Mapper 添加到映射器界面

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

It's a trivial mistake but it is easy to miss这是一个微不足道的错误,但很容易错过

I also faced similar issue when I was using lombok with mapstruct, Its a known issue.当我将 lombok 与 mapstruct 一起使用时,我也遇到了类似的问题,这是一个已知问题。 What worked for me was to add the lombok dependency in the annotationProcessorPaths.对我有用的是在 annotationProcessorPaths 中添加 lombok 依赖项。

<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.请参阅答案以获取完整详细信息。

I checked my plugin version and app (both source and target) version and set them equally.我检查了我的插件版本和应用程序(源和目标)版本并将它们设置为相同。 and it's worked成功了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM