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

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

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:

  1. @Mapper(componentModel = "spring",
  2. collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
  3. public interface InstanceMapper {
  4. @Mapping(target = "startDate", expression = "java(OffsetDateTime.parse(source.getStartDate()))")
  5. InstanceDTO toInstanceDTO(Instance source);
  6. @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
  7. Instance toInstance(InstanceDTO source);
  8. }

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()))"):

  1. [ERROR] project/target/generated-sources/annotations/path/service/mappers/InstanceMapperImpl.java error: cannot find symbol
  2. [ERROR] symbol: variable OffsetDateTime
  3. [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:

  1. @Mapper(componentModel = "spring",
  2. collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
  3. public interface InstanceMapper {
  4. @Mapping(target = "startDate", expression = "java(OffsetDateTime.parse(source.getStartDate()))")
  5. InstanceDTO toInstanceDTO(Instance source);
  6. @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
  7. Instance toInstance(InstanceDTO source);
  8. }

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()))"):

  1. [ERROR] project/target/generated-sources/annotations/path/service/mappers/InstanceMapperImpl.java error: cannot find symbol
  2. [ERROR] symbol: variable OffsetDateTime
  3. [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)来解决这个问题:

  1. @Mapper(componentModel = "spring",
  2. collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
  3. public interface InstanceMapper {
  4. @Mapping(target = "startDate", expression = "java(java.time.OffsetDateTime.parse(source.getStartDate()))")
  5. InstanceDTO toInstanceDTO(Instance source);
  6. @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
  7. Instance toInstance(InstanceDTO source);
  8. }
英文:

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:

  1. @Mapper(componentModel = "spring",
  2. collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
  3. public interface InstanceMapper {
  4. @Mapping(target = "startDate", expression = "java(java.time.OffsetDateTime.parse(source.getStartDate()))")
  5. InstanceDTO toInstanceDTO(Instance source);
  6. @Mapping(target = "startDate", expression = "java(source.getStartDate().toString())")
  7. Instance toInstance(InstanceDTO source);
  8. }

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:

确定