gpt4 book ai didi

java - Mapstruct 映射空对象的属性

转载 作者:行者123 更新时间:2023-12-01 18:26:31 25 4
gpt4 key购买 nike

我在生成映射实现时遇到一个特殊问题,我需要映射空对象的属性。 ObjectRequestMapper中的代码是这样的:

    @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, nullValueCheckStrategy =
NullValueCheckStrategy.ALWAYS, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL, uses = {
WrapperGrpcMapper.class, AddressRequestMapper.class })
public interface ObjectRequestGrpcMapper {

@Mapping(target = "wcsStoreId", source = "storeId")
@Mapping(target = "linesList", source = "lines")
ObjectRequest map(Long storeId, @Context String countryCode, List<ObjectLines> lines, Address address);

ObjectRequestLine lineToProto (ObjectLine line);

}

AddressRequestMapper:

    public interface AddressRequestMapper {

default Address toProto(Address address, @Context String countryCode){
return this.map(address, countryCode);
}

Address map(Address address, String countryCode);
}

当我调用 ObjectRequest 的映射并通过 countryCode = "EN" 传递所有参数并且对象地址为 null 时,AddresRequestMapper 的映射器不适用,因为 ObjectRequestMapper 检查 Address == null,然后不将 ObjectRequest.Address 映射到地址

这种情况有可能得到解决方案吗?

谢谢

最佳答案

如果您需要获取而不是“Address == null”->“new Address()”,只需更改:

nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT

关于java - Mapstruct 映射空对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222009/

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