gpt4 book ai didi

java - 使用 ObjectFactory 时 Mapstruct 无法映射属性

转载 作者:行者123 更新时间:2023-12-02 11:11:04 26 4
gpt4 key购买 nike

我需要一些社区帮助来指出我的代码中的错误...:)

我尝试在@ObjectFactory的帮助下使用mapstruct来映射两个实体之间的字段。

实体1:

public class ScimUser   {

@JsonProperty("addresses")
@Valid
private List<UserAddress> addresses = null;

}

实体2:

public class User   {

@JsonProperty("postalAddress")
private PostalAddress postalAddress = null;

}

映射器:

@Mapper(componentModel = "spring", uses = { AddressFactory.class })
public interface ScimUserMapper {

@Mapping(target = "postalAddress", source = "scimUser.addresses")
User toUser(ScimUser scimUser);

@Mapping(target = "addresses", source = "user.postalAddress")
ScimUser toScimUser(User user);

}

对象工厂:

@Component
public class AddressFactory {

@Autowired
private CountryMapper countryMapper;

@Autowired
private CountryRepository countryRepository;

@ObjectFactory
public PostalAddress toPostalAddress(List<UserAddress> addresses, @TargetType Class<PostalAddress> type) {
PostalAddress postalAddress = new PostalAddress();
if (addresses != null && !addresses.isEmpty()) {
UserAddress userAddress = addresses.stream().filter(UserAddress::isPrimary).findFirst().orElse(null);
if (userAddress == null) {
userAddress = addresses.get(0);
}
postalAddress.setAddressLine1(userAddress.getStreetAddress());
postalAddress.setPostCode(userAddress.getPostalCode());
postalAddress.setState(userAddress.getRegion());
postalAddress.setCity(userAddress.getLocality());
CountryJpa countryJpa = countryRepository.getCountryByIso2Code(userAddress.getCountry());
if (countryJpa != null) {
Country country = countryMapper.fromJPA(countryJpa);
postalAddress.setCountry(country);
}
}
return postalAddress;
}

@ObjectFactory
public List<UserAddress> toUserAddressList(PostalAddress address, @TargetType Class<List<UserAddress>> type) {
UserAddress userAddress = new UserAddress();
userAddress.setCountry(address.getCountry().getIso2());
userAddress.setFormatted("?");
userAddress.setLocality(address.getCity());
userAddress.setPostalCode(address.getPostCode());
userAddress.setPrimary(true);
userAddress.setRegion(address.getState());
userAddress.setStreetAddress(address.getAddressLine1());
userAddress.setType("?");

return Collections.singletonList(userAddress);
}

}

上面的代码在源代码生成过程中出现此错误:

无法将属性“java.util.List 地址”映射到“PostalAddress postalAddress”。考虑声明/实现一个映射方法:“PostalAddress map(java.util.List value)”。无法将属性“PostalAddress postalAddress”映射到“java.util.List 地址”。考虑声明/实现一个映射方法:“java.util.List map(PostalAddress value)”。

这不是我第一次在使用这些对象工厂时遇到困难,而且我真的不明白我做错了什么。

因此,如果有人有想法,我很乐意阅读。 :)

最佳答案

您使用的@ObjectFactory是错误的。您想要实现的是自定义映射方法。

@ObjectFactory 需要用于创建目标实例对象。在您的情况下,如果您只是从方法中删除 @ObjectFactory@TargetType 那么它应该可以正常工作。

我必须强调,您在那里做了相当多的手动映射。您可以轻松提供在单个 UserAddressPostalAddress 之间进行映射的方法,并且只需为集合添加包装器即可。

关于java - 使用 ObjectFactory 时 Mapstruct 无法映射属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50616690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com