【问题标题】:Mapstruct: BeforeMapping not called correctlyMapstruct:未正确调用BeforeMapping
【发布时间】:2022-10-15 18:52:53
【问题描述】:

我有下面的映射器,我在其中使用了 BeforeMapping 注释

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
interface PaymentMapper {

    companion object {
        @JvmStatic
        @BeforeMapping
        fun mapInvoices(@MappingTarget target: Payment, source: PaymentRequested) {
            target.setbillerAuthIds(source.invoices.map { it.billerAuthId })
        }
    }
    
    fun permissionCreatedToPermission(source: PaymentRequested): Payment

}

问题出在实现中,方法mapInvoices在方法的最后被调用

执行

@Component
public class PaymentMapperImpl implements PaymentMapper {

    @Override
    public Payment permissionCreatedToPermission(PaymentRequested source) {
        if ( source == null ) {
            return null;
        }

        String requestNumber = source.getRequestNumber();
        List<String> billerAuthIds = null;

        Payment payment = new Payment( requestNumber, billerAuthIds);

        PaymentMapper.mapInvoices( payment, source );

        return payment;
    }
}

【问题讨论】:

    标签: kotlin mapstruct


    【解决方案1】:

    该方法在调用设置器之前在正确的位置调用(在您的情况下没有设置器)。

    您有一个 before 映射,需要将映射目标 Payment 传递给它。但是,Payment 有一个构造函数, MapStruct 必须调用它才能创建一个实例以传递给生命周期方法。

    如果您想以特殊方式映射billerAuthIds,我建议您为它们提供映射方法。然后 MapStruct 将使用它。

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 2020-12-01
      • 2018-06-25
      相关资源
      最近更新 更多