Mapstruct:从注入的映射器调用函数

时间:2020-05-07 13:02:51

标签: spring mapstruct

我有一个这样的映射器:

    @Mapper(uses = {DateMapper.class, AuthenticationMapper.class})
    public interface LocalUserMapper {
        LocalUserMapper INSTANCE = Mappers.getMapper(LocalUserMapper.class);

        @Mapping(target = "domains", source = "localUser.directDomains")
        @Mapping(target = "phone", source = "localUser.phoneNumber")
        @Mapping(target = "displayName", source = "localUser.displayPartyName")
        @Mapping(target = "authenticationMethod", expression =" java(mapAuthenticationMethodForLocalUser(localUser))")
        UserDTO convertLocalUser(LocalUser localUser);
}

要映射字段“ authenticationMethod”,我想在本地做一些事情,然后让AuthenticationMapper(请参见上文)完成其余工作(注意:我无法更改AuthenticationMapper的主要映射功能来满足我的需要,在其他地方使用) 理想情况下,使用此函数进行映射(作为表达式)

    default String mapAuthenticationMethodForLocalUser(LocalUser localUser) {
            AuthenticationMethod authenticationMethod = localUser.getAuthenticationMethod();
            if (localUser.useTwoLayerAuthentication()) {
// do some stuff and return something
            } //else
            return **authenticationMethodMapper**.convertAuthenticationMethod(authenticationMethod);
        }

这不起作用,因为在生成展示时,authenticationMethodMapper是未知字段

有人知道这样做的好方法吗?谢谢!

0 个答案:

没有答案