作为 GRPC api 的一部分,我正在尝试将 proto 自动生成的类映射到 pojo。这是 .proto 文件
message AccountModelProto
{
repeated VerificationModelProto verification = 1;
}
message VerificationModelProto
{
string status = 1;
string comment = 2;
string verificationType = 3;
repeated VerificationAttributeModelProto verificationAttributes = 4;
}
message VerificationAttributeModelProto
{
string type = 1;
string label = 2;
bool attributeStatus = 3;
}
上述代码的映射器是。我根据它参考了 mapstruct 提供的示例我不需要提供 VerificationModelProto 到 List 的显式映射但是我得到编译器错误
错误:无法将属性“集合验证”映射到“VerificationModelProto 验证列表”。考虑声明/实现一个映射方法:“VerificationModelProto map(Collection value)”。
@Mapping(source = "verification", target = "verificationList", qualifiedByName = "verificationModelToVerificationProtoMapping")
AccountModelProto map(AccountModel accountModel);
@Named("verificationModelToVerificationProtoMapping")
default VerificationModelProto map (VerificationModel verificationModel)
{
VerificationModelProto.Builder builder = VerificationModelProto.newBuilder()
.setComment(verificationModel.getComment())
.setStatus(verificationModel.getStatus().toString())
.setVerificationType(verificationModel.getVerificationType());
for(VerificationAttributeModel verificationAttributeModel: verificationModel.getVerificationAttributes())
{
builder.addVerificationAttributes(getVerificationAttributeBuilder(verificationAttributeModel));
}
return builder.build();
}
default VerificationAttributeModelProto getVerificationAttributeBuilder(VerificationAttributeModel verificationAttributeModel)
{
VerificationAttributeModelProto.Builder builder = VerificationAttributeModelProto.newBuilder()
.setAttributeStatus(verificationAttributeModel.getAttributeStatus())
.setType(verificationAttributeModel.getType())
.setLabel(verificationAttributeModel.getLabel());
return builder.build();
}
如何度过这个难关。我将 CollectionMappingStrategy 添加为 CollectionMappingStrategy.ADDER_PREFERRED。