ElP*_*ter 7 java mapping maven spring-boot mapstruct
使用最新的 Springboot 和 MapStruct 版本并使用 Maven 构建,我正在尝试实现官方 MapStruct 站点中给出的“从这里开始”示例
我的代码更简单:
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>
Run Code Online (Sandbox Code Playgroud)
Car.java
public class Car {
private String model;
// Constructors, setters and getters...
}
Run Code Online (Sandbox Code Playgroud)
卡托.java
public class CarDto {
private String theModel;
// Constructors, setters and getters...
Run Code Online (Sandbox Code Playgroud)
}
CarMapper.java 接口
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
@Mapping(source = "model", target = "theModel")
CarDto carToCarDto(Car car);
}
Run Code Online (Sandbox Code Playgroud)
主要应用
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
所有代码都在这个公共仓库中: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
Run Code Online (Sandbox Code Playgroud)
我在官方的 MapStruct 项目中发现了这个问题,它似乎描述了同样的问题。但是,在这种情况下,正在执行一些自定义配置(实现的自定义名称)。在我的情况下,一切都保留为默认值。
任何的想法?
小智 24
我添加了 mapstruct 处理器依赖项,它对我有用
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
Joã*_*tos 22
尽管我的情况与您的情况不同,但它确实导致了相同的错误 - 因此我发布此答案是为了帮助其他犯了与我相同错误并最终在这里寻找解决方案的人。
我正在导入 Maven 依赖项:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但忘记在maven编译器插件中添加注释处理器路径:
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
Run Code Online (Sandbox Code Playgroud)
当我在项目上运行给定的命令时,我的问题得到了解决 -
mvn clean install
Run Code Online (Sandbox Code Playgroud)
然后我注意到正在生成实现文件。我猜想生成实现需要执行 Maven 命令。
小智 5
当我将 lombok 与 mapstruct 一起使用时,我也遇到了类似的问题,这是一个已知问题。对我有用的是在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>
Run Code Online (Sandbox Code Playgroud)
请参阅此答案以获取完整的详细信息。
| 归档时间: |
|
| 查看次数: |
12451 次 |
| 最近记录: |