具有两个不同来源的Mapstruct

问题描述 投票:0回答:1

我正在尝试在源和目标之间进行映射。

@Data
@Builder
@Getter
public class Source {
    public String str3;
}

@Data
@Builder
@Getter
public class Target {
  public String str1;
  public String str2;
  public AnotherObj anotherObj;
}

@Data
@Builder
@Getter
public class AnotherObj {
  public String str3;
  public String str4;
}

我有一个这样的映射器

@Mapping(target = "anotherObj", source = "request", qualifiedByName = "mapAnotherObjToSource")
@Mapping(target = "anotherObj.str4", source = "str4")
Target convert(Source request, String str4);

但是当我这样做时,我收到如下错误:

Java: No read accessor found for property anotherObj in target type.

我有吸气剂,可能缺少什么?

我尝试像这样使用

@AfterMapping

@AfterMapping
default void handleStr4(@MappingTarget Target target, Source source, String str4) {
if (target.getAnotherObj() != null) {
    target.getCardMetadataInfo().setStr4(str4);
    }
}
java mapstruct
1个回答
0
投票

尝试使用 setter 方法进入目标源:

@Data
@Builder
@Getter
public class Target {
  public String str1;
  public String str2;
  public AnotherObj anotherObj;

  public AnotherObj getAnotherObj() {
      return anotherObj;
  }

  public void setAnotherObj(AnotherObj anotherObj) {
      this.anotherObj = anotherObj;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.