首页 > 解决方案 > 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);
        }

这不起作用,因为生成 impl 时 authenticationMethodMapper 不知何故是未知字段

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

标签: springmapstruct

解决方案


推荐阅读