英文:
Inner immutable class with mapStruct
问题
我们正在使用Immutables与MapStruct,在将实体转换为DTO时遇到了问题。
项目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); // 仅当注释掉project statistics的内部模型时有效。
//ProjectDto toDto(Project project); 这不起作用(错误1)
// ImmProjectDto toDto(ImmProject project); 在取消注释project statistics的内部类后,即使这也不起作用(错误2)
}
在出现错误的情况下,问题是完全相同的:
错误1: 由于在错误元素com.xyz.ProjectDto中出现问题,因此未为ProjectMapper创建任何实现。
错误2: 由于在错误元素com.xyz.ProjectStatisticsDto中出现问题,因此未为ProjectMapper创建任何实现。
我检查了关于使用Immutables和MapStruct的测试,没有看到任何不同之处。https://github.com/mapstruct/mapstruct/blob/master/integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Person.java。
我尝试删除了序列化语句,但没有运气。我添加了一些详细的语句,显示:
注释: MapStruct: 类路径上发现Immutables
注释: MapStruct: 使用访问器命名策略: org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy
注释: MapStruct: 使用构建器提供程序: org.mapstruct.ap.spi.ImmutablesBuilderProvider
注释: MapStruct: 使用枚举命名策略: org.mapstruct.ap.spi.DefaultEnumMappingStrategy
这看起来绝对正确。
英文:
We are using Immutables with MapStruct and ran into a problem while converting an entity to 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();
}
The mapper 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)
In the cases of error, the issue is exactly the same
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.
I checked the tests for mapstruct with immutables and there is nothing different I see https://github.com/mapstruct/mapstruct/blob/master/integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Person.java.
I tried removing serialization statements but no luck. I added some verbose statements which say
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
And this looks absolutely correct
答案1
得分: 1
看到问题的标题“具有MapStruct的内部不可变类”,我猜想你的不可变类位于另一个类内部。
这是MapStruct已知的问题(参见mapstruct/mapstruct#2198),该问题已经有一个PR,并将在下一个非补丁发布中修复。
与此同时,你需要将不可变类改为顶层类。
英文:
Looking at the title of the question "Inner immutable class with mapStruct" I guess that your immutable classes are inside another class.
This is a known problem for MapStruct (see mapstruct/mapstruct#2198) that already has a PR for it and it will be fixed in the next non patch release.
In the meantime you will have to make your Immutable classes top level classes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论