内部不可变 class 与 mapStruct

Inner immutable class with mapStruct

我们将 Immutables 与 MapStruct 一起使用,运行 在将实体转换为 dto 时遇到问题。

@Value.Immutable
public interface ProjectDto {
    String getId();
    String getName();
    //ProjectStatisticsDto getStatistics();
}

@Value.Immutable
public interface ProjectStatisticsDto {
    Long getCount();
}

@Immutable
public interface Project extends Serializable {
    @JsonProperty("_id")
    String getId();
    String getName();
    //ProjectStatistics getStatistics();
}

@Immutable
public interface ProjectStatistics extends Serializable {
    Long getCount();
}

映射器class

@Mapper
public interface ProjectMapper {
    ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class);
    ImmProjectDto toDto(ImmProject project); // This works only when the inner model of project statistics is commented.
    //ProjectDto toDto(Project project); THIS DOES NOT WORK (Error 1)
    // ImmProjectDto toDto(ImmProject project); After I uncomment the inner class of project statistics then even this does not work (Error 2)

在错误的情况下,问题完全相同

Error 1: No implementation was created for ProjectMapper due to having a problem in the erroneous element com.xyz.ProjectDto. 
Error 2: No implementation was created for ProjectMapper due to having a problem in the erroneous element com.xyz.ProjectStatisticsDto. 

我检查了带有不可变对象的 mapstruct 测试,我发现没有什么不同 https://github.com/mapstruct/mapstruct/blob/master/integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Person.java

我尝试删除序列化语句,但没有成功。我添加了一些冗长的语句

Note: MapStruct: Immutables found on classpath
Note: MapStruct: Using accessor naming strategy: org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy
Note: MapStruct: Using builder provider: org.mapstruct.ap.spi.ImmutablesBuilderProvider
Note: MapStruct: Using enum naming strategy: org.mapstruct.ap.spi.DefaultEnumMappingStrategy

这看起来完全正确

查看问题“Inner immutable class with mapStruct”的标题,我猜你的不可变 classes 在另一个 class.

这是 MapStruct 的一个已知问题(参见 mapstruct/mapstruct#2198),它已经有一个 PR,将在下一个非补丁版本中修复。

与此同时,您必须使您的不可变 classes 成为顶级 classes。