-1

I try to convert one complex object to another complex object. All the object have sub-object. In theses object, i have date with differents format...

Look at that :

ObjectA : {
  Long dateA,
  CustomObject: {
    Long dateB
    List<CustomObject2> myObject
  }
}

CustomObject2 : {
  Long dateC
}

And then my destination object :

ObjectB : {
  LocalDateTime dateA,
  CustomObject: {
    LocalDateTime dateB
    List<CustomObject3> myObject
  }
}

CustomObject3 : {
  LocalDateTime dateC
}

I have write mapstruct converter to convert ObjectA to ObjectB:

@Mapping( target = "dateA", qualifiedByName = "toLocalDateTime")
ObjectB convertROToBO(ObjectA in);

@Named("toLocalDateTime")
static LocalDateTime toLocalDate(final Long dateMilliseconds) {

    if (dateMilliseconds == null) {
        return null;
    }

    return Instant.ofEpochMilli(dateMilliseconds).atZone(ZoneId.systemDefault()).toLocalDateTime();

}

So, when i convert my ObjectA to ObjectB, only my dateA is converted (my dateB @ dateC are not converted).

*** How can i convert all my Long to LocalDateTime recursively with map struct ? Is it possible to do that with single converter and single convertion method ? ***

Thanks :)

Jérémy
  • 391
  • 2
  • 7
  • 20

1 Answers1

0

The reason why your toLocalDate is not recursively picked up is because you are using @Named, when you qualify your methods they can only be used in combination with qualifiedByName and qualifiedBy.

If you remove @Named from the toLocalDate then all mappings between Long and LocalDateTime will use that method.

Filip
  • 19,269
  • 7
  • 51
  • 60