MapStruct 转换错误类型不兼容:无法转换为

时间:2021-01-23 18:07:05

标签: java spring mapping lombok mapstruct

不确定这里会发生什么,但是当我执行 mvn clean install 时,我收到以下错误:

不兼容的类型:capture#1 of ?无法转换为 com.sweetchoice.report.entities.WhoDelivers

映射器类:

@Mapper(uses = {BaseJournalMapper.class})
public interface WhoDeliversMapper {

    WhoDelivers dtoToEntity(WhoDeliversDto whoDeliversDto);

    WhoDeliversDto entityToDto(WhoDelivers whoDelivers);
}

实体类:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "who_delivers")
public class WhoDelivers extends BaseJournalEntity{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private Long id;

    private String name;
}

BaseJournalEntity/BaseJournalDto:

@Getter
@Setter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public class BaseJournalDto {

    private LocalDateTime createdAt;
    private Long createdBy;
    private LocalDateTime updatedAt;
    private Long updatedBy;
}

DTO 类:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class WhoDeliversDto extends BaseJournalDto{

    private Long id;

    private String name;

}

最后是 pom.xml:

     <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId> lombok-mapstruct-binding</artifactId>
                            <version>0.1.0</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>
enter code here

我使用的是 Java 11、MapStruct 1.4.1.Final 和 Lombok 1.18.16

1 个答案:

答案 0 :(得分:0)

答案:

我自己找到了答案。

只需在 Entity 和 DTO 类中添加 @SuperBuilder 即可。有关此链接的更多信息 Documentation