从平坦的Protobuf消息映射到使用Mapstruct的内部类的类
我需要使用mapsstruct
将从消息addressProto
的映射写入class
地址。但是消息addressProto
仅由字符串字段组成,而地址
类具有内部类。
到目前为止,我已经写了这样的映射器,但是由于消息结构和类之间的差异,我不知道如何正确映射从消息addressproto
class 地址
地址和后背。
@Mapper(config = MapstructConfig.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
public abstract class AddressProtoMapper {
// from proto to object
public abstract Address mapToAddress(AddressProto address);
// from object to proto
public abstract AddressProto mapAddressToProto(Address address);
}
proto Message addressProto
(每个字段的斜线之后,我在哪个类字段中都需要映射):
message AddressProto {
string value = 1; // Address.AddressValue.value
string unrestricted_value = 2; // Address.AddressValue.unrestrictedValue
string country = 3; // Address.Structure.Country.name
string country_iso_code = 4; // Address.Structure.Country.isoCode
string region = 5; // Address.Structure.Region.name
}
Java class address> address
:
public class Address {
public final AddressValue value;
public final Structure structure;
public static class AddressValue {
public final String value;
public final String unrestrictedValue;
}
public static class Structure {
public final Country country;
public final Region region;
public static class Country {
public final String name;
public final String isoCode;
}
public static class Region {
public final String name;
}
}
}
I need to use mapStruct
to write a mapping from message AddressProto
to the class
Address. But message AddressProto
consists only of string fields while Address
class has inner classes.
I have written such a mapper so far, but due to the difference between the message structure and the class, I don’t know how to correctly map fields from message AddressProto
to class Address
and back.
@Mapper(config = MapstructConfig.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
public abstract class AddressProtoMapper {
// from proto to object
public abstract Address mapToAddress(AddressProto address);
// from object to proto
public abstract AddressProto mapAddressToProto(Address address);
}
proto message AddressProto
(after the slash for each field, I wrote in which class field it needs to be mapped):
message AddressProto {
string value = 1; // Address.AddressValue.value
string unrestricted_value = 2; // Address.AddressValue.unrestrictedValue
string country = 3; // Address.Structure.Country.name
string country_iso_code = 4; // Address.Structure.Country.isoCode
string region = 5; // Address.Structure.Region.name
}
java class Address
:
public class Address {
public final AddressValue value;
public final Structure structure;
public static class AddressValue {
public final String value;
public final String unrestrictedValue;
}
public static class Structure {
public final Country country;
public final Region region;
public static class Country {
public final String name;
public final String isoCode;
}
public static class Region {
public final String name;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案非常简单。甚至太多:)
您只需要编写自己的实现即可。
The solution turned out to be very simple. Even too much :)
You just need to write your own implementation.