防止Mapstruct在自动映射器检测中使用方法

问题描述 投票:0回答:2

我有一个 Mapstruct 映射器,我必须在其中执行一些字符串转换服务。即,从自然语言短语列表到实用程序类中定义的不透明常量。不,这次我不会使用枚举。它涉及到一些预定义措辞列表的编辑检查。

我在 Spring bean 中有一个方法

String getSyntheticDescription(String description)
,我想用它来映射我的 DTO 中的 single 字符串字段。

如果我在

uses
中使用Mapstruct的
@Mapping
属性,我发现Mapstruct会在字符串转换周围滥用
definedBean.getSyntheticDescription
。基本上每个映射到字符串的字符串字段都会传递给
definedBean.getSyntheticDescription
,这显然不是我想要的。

我只想在单个字段的

expression
属性中使用它。

如何告诉 Mapstruct 不要尝试使用所有可用的映射方法并特别忽略

getSyntheticDescription
,除非另有说明?

代码

@Mapper(componentModel = "spring", uses = {TaxonomyStringParser.class, TaxonomyCustomerLogic.class})
public interface TaxonomyControlMapping {

    @Mapping(target = "notcompliant", source = "nonConforme")
    @Mapping(target = "withurgency", source = "nonConformeConCriticita")
    @Mapping(target = "compliant", source = "conforme")
    @Mapping(target = "perimeter", expression = "java(taxonomyCustomerLogic.getControlPerimeterValueFromDescription(dto.getPerimetroAnalisi()))")
    @Mapping(target = "sampling", source = "campionamento")
    @Mapping(target = "performer", source = "performer")
    TaxonomyControlVersion fromMasterDbDto(TaxonomyControlMasterDbDTO dto) throws ParseException;


}

结果

        taxonomyControlVersion.setInstructions( taxonomyCustomerLogic.getControlPerimeterValueFromDescription( dto.getIndicazioniValutazione() ) );
        taxonomyControlVersion.setSignificance( taxonomyCustomerLogic.getControlPerimeterValueFromDescription( dto.getSignificativita() ) ); //BAD
        taxonomyControlVersion.setSamplingmode( taxonomyStringParser.parseSamplingModeType( dto.getModalitaCampionamento() ) ); //BAD

        taxonomyControlVersion.setPerimeter( taxonomyCustomerLogic.getControlPerimeterValueFromDescription(dto.getPerimetroAnalisi()) ); //GOOD


mapstruct
2个回答
4
投票

为了使方法仅在特定用例中使用,需要使用

@Named
中的
org.mapstruct
或同样来自
@Qualifier
的自定义
org.mapstruct
进行注释。

在您的情况下,您可以将

@Named("syntheticDescription")
添加到 Spring bean 中的方法,然后使用
Mapping#qualifiedByName

例如

@Mapper(componentModel = "spring", uses = {TaxonomyStringParser.class, TaxonomyCustomerLogic.class})
public interface TaxonomyControlMapping {

    @Mapping(target = "notcompliant", source = "nonConforme")
    @Mapping(target = "withurgency", source = "nonConformeConCriticita")
    @Mapping(target = "compliant", source = "conforme")
    @Mapping(target = "perimeter", source = "perimetroAnalisi" qualifiedByName = "syntheticDescription" )
    @Mapping(target = "sampling", source = "campionamento")
    @Mapping(target = "performer", source = "performer")
    TaxonomyControlVersion fromMasterDbDto(TaxonomyControlMasterDbDTO dto) throws ParseException;


}

0
投票

我解决了通过 setter 注入 bean 并从 Mapper(use) 属性中删除它的问题

示例:

@Mapper(
    config = CentralConfig.class
    //do not write here uses = Service.class
)
public abstract class AccountMapper {

    protected Service service;
    @Autowired
    public void setService(Service service) {
        this.service = service;
    }
}

然后mapper只使用我问他的一种方法@Mapping(表达式)

© www.soinside.com 2019 - 2024. All rights reserved.