【发布时间】:2020-08-25 20:40:09
【问题描述】:
使用最新的 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 接口
@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);
}
}
所有代码都在这个公共仓库中: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
我在官方 MapStruct 项目中找到了this issue,它似乎描述了同样的问题。但是,在这种情况下,正在执行一些自定义配置(实现的自定义名称)。在我的情况下,一切都保留为默认值。
有什么想法吗?
【问题讨论】:
-
你找到解决办法了吗?
标签: java spring-boot maven mapping mapstruct