ModelMapper转换器问题?

时间:2018-10-25 03:51:11

标签: modelmapper

我有如下方法:

public Page<FindUserByIdOutput> FindAll(Pageable pageable) {

        Page<UsersEntity> foundUsers = _userManager.FindAll(pageable);
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        java.lang.reflect.Type targetListType = new TypeToken<Page<FindUserByIdOutput>>() {}.getType();


        Converter<UsersEntity, FindUserByIdOutput> myConverter = new Converter<UsersEntity, FindUserByIdOutput>()
        {
            public FindUserByIdOutput convert(MappingContext<UsersEntity, FindUserByIdOutput> context)
            {
                UsersEntity s = context.getSource();
                FindUserByIdOutput d = context.getDestination();

                Set<PermissionId> result = new HashSet<PermissionId>();
                s.getPermissions().forEach(permission -> result.add(new PermissionId(permission.getId())));

                d.setPermissions(result);
                d.setRole(new RoleId((s.getRole().getId())));
                return d;
            }
        };

        modelMapper.addConverter(myConverter);

        Page<FindUserByIdOutput> output = modelMapper.map(foundUsers, targetListType);
        modelMapper.validate();
        return output;
    }

此方法返回的输出“输出”看起来与Page<UsersEntity>相似,而不是

Page<FindUserByIdOutput>
  • 换句话说,转换器不能正常工作。

注意:我正在将PermissionsEntity集合转换为PermissionId对象的集合。同样,我正在将RoleEntity转换为RoledId对象。

您能看到上面的代码有什么问题吗?

0 个答案:

没有答案