MapStruct - 找不到实现

问题描述 投票:0回答:1

使用最新的Springboot和MapStruct版本,并使用Maven构建,我正在尝试实现以下例子中的 "Start Here"。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>

汽车.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);

    }

}

所有的代码都在这个公共repo中。https:/github.compgboninomappertest。

运行时,我得到了这个错误。

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项目中,似乎描述了同样的问题。然而,在这种情况下,一些自定义的配置被执行(实现的自定义名称)。在我的案例中,所有的东西都是默认的。

有什么办法吗?

java spring-boot mapping mapstruct
1个回答
0
投票

你需要确保你的IDE被正确配置以调用注解处理器。请看一下 集成开发环境设置.

从你提供的项目来看,代码应该连编译都没有。MapStruct处理器会发出编译错误,原因是。

  • 在MapStruct中没有默认的构造函数 CarDto
  • 财产 model 不存在 Car (只有 marca)
  • 财产 theModel 不存在 CarDto (只有 laMarca)
© www.soinside.com 2019 - 2024. All rights reserved.