mapstruct强制转换错误不兼容的类型:无法转换为

thigvfpy  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(1032)

不知道这里会发生什么,但我知道的时候 mvn clean install 我得到以下错误:
不兼容类型:捕获#1个?无法转换为com.sweetchoice.report.entities.whodelivers
Map器类:

@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/基本日志收件人:

@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

我使用的是Java11、MapStruct1.4.1.final和Lombok1.18.16

hc8w905p

hc8w905p1#

回答:
我自己找到了答案。
只需在entity和dto类中添加@superbuilder。有关此链接文档的详细信息

相关问题