简体   繁体   中英

Using generics with @Named in mapstruct 1.4.1

We had been using mapstruct 1.3.1 and the below construct in our mapper class would work

@Data
public class SourceClass {
  private SomeClass1 field1;
  private SomeClass2 field2;
  private SomeClassN field4;
}

@Data
public class TargetField {
  private SomeClass1 field1;
  private SomeClass2 field2;
  private SomeClassN field3;
}

@Data
public class TargetClass {
  private Optional<TargetField> targetField = Optional.empty();
}

public interface MyMapper {
  @Mapping(target = "field3", source = "field4")
  TargetField mapParty(SourceClass source);
  
  @Mapping(target = "targetField", source = "source", qualifiedByName = "wrapAsOptional")
  void mapParticipant(@MappingTarget TargetClass target, SourceClass source);
  
  @Named("wrapAsOptional")
  default <T> Optional<T> wrapAsOptional(T data) {
    return Optional.ofNullable(data);
  }
}

After upgrading to 1.4.1, I get the below error

Qualifier error. No method found annotated with @Named#value: [ wrapAsOptional ]. See https://mapstruct.org/faq/#qualifier for more info.
Can't map parameter "SourceClass source" to "Optional<TargetField> targetField". Consider to declare/implement a mapping method: "Optional<TargetField> map(SourceClass value)".

Is this expected? How do I make this working again? We use Java 11 in our project

Hmmm, something seems wrong here:

Can't map parameter "MyClassA source" to "Optional<MyClassB>"

Why does it try to map MyClassA to Optional< MyClassB >. Obviously it cannot be done with the wrapAsOptional method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM