使用最新的 Springboot 和 MapStruct 版本并使用 Maven 进行构建,我正在尝试实现 the official MapStruct site 中给出的“从这里开始”示例
我的代码更简单:
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>
汽车.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);
}
主要应用
@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);
}
}
所有代码都在这个公共(public)存储库中:https://github.com/pgbonino/mappertest
运行时,我收到此错误:
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
我找到了this issue在官方 MapStruct 项目中,这似乎描述了同样的问题。但是,在这种情况下,正在执行一些自定义配置(实现的自定义名称)。就我而言,一切都保留为默认值。
有什么想法吗?
最佳答案
尽管我的情况与您的情况不同,但它确实导致了相同的错误 - 因此我发布此答案是为了帮助其他犯了与我相同错误并最终在这里寻找解决方案的人。
我正在导入 Maven 依赖项:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
但是忘记在maven编译器插件中添加注解处理器路径:
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
关于java - MapStruct - 找不到实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61710510/