【发布时间】:2022-05-15 06:50:24
【问题描述】:
我正在尝试从 Protobuf 映射到 POJO。这是 POJO:
public class CreateNodesAllDataDto {
private List<UpsertNodeDto> tables;
private List<UpsertColumnDto> columns;
}
其中upsertNodeDto 如下:
public class UpsertNodeDto {
@NotNull
private String workspaceId;
@NotNull
private String id;
@NotNull
private String name;
}
这是 Protobuf:
message UpsertNodeAllDataInput {
repeated UpsertTableInput tables = 1;
repeated UpsertColumnInput columns = 2;
}
message UpsertTableInput {
string workspace_id = 1;
string id = 2;
string name = 3;
}
你明白了。
现在的问题是 MapStruct 没有映射 Protobuf 中的 repeated 字段,因为生成的 Java 文件中这些字段的 getter 名称为 xxx-list。例如,从UpsertNodeAllDataInput 获取tables 的方式是input.getTablesList() 而不是input.getTables(),显然MapStruct 不承认这一点。
首先肯定不存在 Protobuf 生成问题,因为ModelMapper,一个类似用途的包,在转换UpsertNodeAllDataInput 和CreateNodesAllDataDto 中工作。这也不是 MapStruct 安装的问题,因为它适用于我的 POJO 到 POJO 映射。问题只是上面描述的一个。
我已经环顾了一段时间,只找到了将 POJO 映射到 Protobuf 的答案。 this 之类的东西似乎不起作用。
【问题讨论】:
标签: java mapping protocol-buffers mapstruct grpc-java