使用 @Mapping 和表达式来解析 OffsetDateTime

huangapple go评论78阅读模式
英文:

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);
}

huangapple
  • 本文由 发表于 2023年7月18日 16:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定