【问题标题】:Reuse decorated mapper with Mapstruct使用 Mapstruct 重用修饰的映射器
【发布时间】:2019-05-25 21:25:03
【问题描述】:

我正在使用 MapStruct 1.3.0.Final 将 Dto 转换为 POJO,反之亦然,将 spring 值转换为 mapstruct.defaultComponentModel 选项。为了避免由于双向关系导致的 stackoverflow 错误,我点击了下一个链接:

Mapping With Cycles

其中一些转换器已按照以下说明进行了修饰:

Customizing Mappers Using Decorators

问题是:

如何使用uses选项在其他转换器中使用修饰转换器?

按照第二个链接中包含的示例,我进行了测试:

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

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

但我还没有实现OtherMapper 使用PersonMapperDecorator

【问题讨论】:

    标签: java spring mapstruct


    【解决方案1】:

    将 MapStruct 装饰器与 Spring 组件模型一起使用时,您只需要使用映射器,而不需要使用装饰器。

    在您的示例中,您应该使用

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

    这将注入 PersonMapper 装饰器和注入的原始映射器 (PersonMapper)。

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2018-09-15
      • 2020-08-02
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多