【问题标题】:Inner immutable class with mapStruct带有 mapStruct 的内部不可变类
【发布时间】:2020-10-15 14:26:49
【问题描述】:

我们在 MapStruct 中使用 Immutables,在将实体转换为 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();
}

映射器类

@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

这看起来完全正确

【问题讨论】:

    标签: java immutability mapstruct immutables-library


    【解决方案1】:

    看看问题“Inner immutable class with mapStruct”的标题,我猜你的不可变类在另一个类中。

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

    与此同时,您必须将 Immutable 类设为顶级类。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2022-08-19
      • 2020-10-29
      • 2015-11-14
      • 2020-06-05
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多