英文:
Use @Mapping with expression to parse OffsetDateTime
问题
I am trying to write a Mapper between my internal Entity class and my DTO class using org.mapstruct.Mapper. The Entity class contains a field startDate of type String that is mapped to a field startDate of type OffsetDateTime.
To map those fields I am using @Mapping with an expression like this:
@Mapper(componentModel = "spring",
        collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
public interface InstanceMapper {
    @Mapping(target = "startDate", expression = "java(OffsetDateTime.parse(source.getStartDate()))")
    InstanceDTO toInstanceDTO(Instance source);
    @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
    Instance toInstance(InstanceDTO source);
}
The second @Mapping compiles correctly but at compilation I get the following error because of the @Mapping(target = "startDate", expression = "java(OffsetDateTime.parse(source.getStartDate()))"):
[ERROR] project/target/generated-sources/annotations/path/service/mappers/InstanceMapperImpl.java error: cannot find symbol
[ERROR]   symbol:   variable OffsetDateTime
[ERROR]   location: class InstanceMapperImpl
Adding an unused import to the InstanceMapper.java file does not solve the error, as it seems to be removed at compilation.
英文:
I am trying to write a Mapper between my internal Entity class and my DTO class using org.mapstruct.Mapper. The Entity class contains a field startDate of type String that is mapped to a field startDate of type OffsetDateTime.
To map those fields I am using @Mapping with an expression like this:
@Mapper(componentModel = "spring",
        collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
public interface InstanceMapper {
    @Mapping(target = "startDate", expression = "java(OffsetDateTime.parse(source.getStartDate()))")
    InstanceDTO toInstanceDTO(Instance source);
    @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
    Instance toInstance(InstanceDTO source);
}
The second @Mapping compiles correctly but at compilation I get the following error because of the @Mapping(target = "startDate", expression = "java(OffsetDateTime.parse(source.getStartDate()))"):
[ERROR] project/target/generated-sources/annotations/path/service/mappers/InstanceMapperImpl.java error: cannot find symbol
[ERROR]   symbol:   variable OffsetDateTime
[ERROR]   location: class InstanceMapperImpl
Adding an unused import to the InstanceMapper.java file does not solve the error, as it seems to be removed at compilation.
答案1
得分: 1
由于错误基本上是一个缺少导入的问题,我相当确定您可以通过在表达式中使用完全限定的名称(即java.time.OffsetDateTime)来解决这个问题:
@Mapper(componentModel = "spring",
        collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
public interface InstanceMapper {
    @Mapping(target = "startDate", expression = "java(java.time.OffsetDateTime.parse(source.getStartDate()))")
    InstanceDTO toInstanceDTO(Instance source);
    @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
    Instance toInstance(InstanceDTO source);
}
英文:
Seeing as the error is basically a missing import, I'm reasonably sure you can solve this by using a fully qualified name (i.e. java.time.OffsetDateTime) in your expression:
@Mapper(componentModel = "spring",
        collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
public interface InstanceMapper {
    @Mapping(target = "startDate", expression = "java(java.time.OffsetDateTime.parse(source.getStartDate()))")
    InstanceDTO toInstanceDTO(Instance source);
    @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
    Instance toInstance(InstanceDTO source);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论