How to use @Qualifier or @Named with @AfterMapping in mapstruct?

笑着哭i 提交于 2019-12-08 07:20:25

问题


In their documentation (here: http://mapstruct.org/documentation/dev/api/org/mapstruct/AfterMapping.html), they mention that @AfterMapping can be used with @Qualifier / @Named to filter, but I can't find it anywhere how to actually use it this way.

My best guess was to use it like this:

@Mapper
public abstract class CustomerMapper {

    @Named("Test")
    public abstract Customer map(CustomerDto dto);

    @Named("Test")
    @AfterMapping
    public void doAfterMapping(@MappingTarget Customer customer) {
        //do stuff
    }
}

But that seems to do nothing (If I remove the @Named annotations it works, but it is also used in other methods, which I don't want).. Does anyone know how this needs to be used?


回答1:


I got help in their gitter chatrooms, if anyone is looking for the same thing, this is doable with @BeanMapping like this:

@Mapper
public abstract class CustomerMapper {

    @BeanMapping(qualifiedByName = "Test")
    public abstract Customer map(CustomerDto dto);

    @Named("Test")
    @AfterMapping
    public void doAfterMapping(@MappingTarget Customer customer) {
        //do stuff
    }
}


来源:https://stackoverflow.com/questions/52746320/how-to-use-qualifier-or-named-with-aftermapping-in-mapstruct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!