【问题标题】:Mapstruct - Multiple parameters to inner classMapstruct - 内部类的多个参数
【发布时间】:2022-08-19 16:30:37
【问题描述】:

我正在尝试映射到内部类,但它不起作用。

我有以下 Pojos:

public record Author(UUID id, String name) {}

public record Book(Author author) {}

这是映射器:

@Mapper
public interface BookMapper {
    @Mapping(target=\"author\", source=\".\");
    Book map(UUID id, String name);

    Author map(UUID id, String name);
}

但是编译时出现此错误:

BookMapperImpl is not abstract and does not override abstract method map(UUID,String)

任何帮助表示赞赏。

谢谢

  • “非抽象”错误是您得到的唯一错误吗?我想还有一些其他错误,因为使用 source = \".\" 并不能真正用于多源映射方法
  • 我只是得到那个错误。您可能知道的任何解决方法?

标签: mapstruct


【解决方案1】:

BookMapperImpl 不是抽象的,不会覆盖抽象方法 map(UUID,String)

这是一个奇怪的错误,这意味着 MapStruct 由于其他原因生成了一些部分实现,但它没有报告添加错误。这很可能是 MapStruct 中的一个错误。

然而,使用source = "." 不适用于多个源参数,即使没有它也无法将映射传递给Author 映射。

我建议使用自定义方法进行映射,例如

@Mapper
public interface BookMapper {
    @Mapping(target="author", source=".");
    default Book map(UUID id, String name) {
        Author author = map(id, name);
        return author != null ? new Book(author) : null;
    }

    Author map(UUID id, String name);
}

【讨论】:

    猜你喜欢
    • 2018-05-21
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多