Mapstruct为空对象映射属性

时间:2020-02-14 07:45:05

标签: java dictionary mapstruct

我在生成地图实现时有一个特殊的问题,我需要映射一个空对象的属性。代码在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会检查{ {1}},然后不将Address == null映射到地址

有可能针对这种情况找到解决方案吗?

谢谢

1 个答案:

答案 0 :(得分:0)

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

nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT