用于映射嵌套类对象和列表对象的MapStruct



我正在尝试将MapStruct用于类似于以下结构的结构:

来源:

public class ClassSource{
private int id;
private String name;
private String numT; 
private List<CustAddress> addresses = null;
private Person person;
}
public class CustAddress implements Serializable {
@JsonProperty("postalCode")
private String postalCode;
}
public class Person implements Serializable {
@JsonProperty("jobTitle")
private String jobTitle;
}

目的地:

public class ClassDestination {
private int id;
private String name;
private String numT;
private List<CustAddress> addresses = null;
}
public class CustAddress implements Serializable {
@JsonProperty("zipCode")
private String zipCode;
}
public class Person implements Serializable {
@JsonProperty("workTitle")
private String workTitle;
}

想要实现以下从地址列表postalcode到目的地址列表zipcode和子类上的Person jobTitle到目标Person workTitle 的映射

public interface PersonMapper {
@Mapping(source = "addresses.postalCode", target = "addresses.zipCode")
@Mapping(source = "person.jobTitle", target = "person.workTitle")
ClassDestination  map(ClassSource source);

上面的@映射没有正确地将子类从源引用到目标。。它抛出异常

@Getter
@Setter
@Builder
static class ClassSource {
private int id;
private String name;
private String numT;
private List<AddressSource> addresses;
private PersonSource person;
}
@Getter
@Setter
@Builder
static class AddressSource implements Serializable {
@JsonProperty("postalCode")
private String postalCode;
}
@Getter
@Setter
@Builder
static class PersonSource implements Serializable {
@JsonProperty("jobTitle")
private String jobTitle;
}
@Getter
@Setter
@Builder
@ToString
static class ClassDestination {
private int id;
private String name;
private String numT;
private List<AddressDestination> addresses;
private PersonDestination person;
}
@Getter
@Setter
@Builder
@ToString
static class AddressDestination implements Serializable {
@JsonProperty("zipCode")
private String zipCode;
}
@Getter
@Setter
@Builder
@ToString
static class PersonDestination implements Serializable {
@JsonProperty("workTitle")
private String workTitle;
}
@Mapper(uses = {AddressMapper.class})
interface ClassMapper {
@Mapping(source = "person.jobTitle", target = "person.workTitle")
ClassDestination sourceToDestination(ClassSource source);
}
@Mapper
interface AddressMapper {
@Mapping(source = "postalCode", target = "zipCode")
AddressDestination sourceToDestination(AddressSource source);
}
private static final ClassMapper classMapper = Mappers.getMapper(ClassMapper.class);
public static void main(String[] args) {
PersonSource personSource = PersonSource.builder().jobTitle("Dev").build();
List<AddressSource> addressSources =
List.of(
AddressSource.builder().postalCode("412101").build(),
AddressSource.builder().postalCode("411001").build());
ClassSource classSource =
ClassSource.builder()
.id(1)
.name("Firstname")
.numT("2")
.addresses(addressSources)
.person(personSource)
.build();
ClassDestination classDestination = classMapper.sourceToDestination(classSource);
System.out.println(classDestination);
}

我在本地创建了一个临时场景。我使用了一个额外的映射器来映射嵌套对象。这应该行得通。

这可能是您的问题:

CCD_ 1。

如果没有zipCode字段,则addressesList。您需要为人员声明一个单独的方法来声明映射,

public interface AddressMapper {
@Mapping(source = "person.jobTitle", target = "person.workTitle")
ClassDestination  map(ClassSource source);
@Mapping(source = "postalCode", target = "zipCode")
destination.Address mapPerson(source.Address);
}

相关内容

  • 没有找到相关文章

最新更新