简体   繁体   English

使用 mapStruct 从平面 protobuf 消息映射到具有内部类的类

[英]mapping from flat protobuf message to class with inner classes using mapStruct

I need to use mapStruct to write a mapping from message AddressProto to the class Address.我需要使用mapStruct编写从消息AddressProtoclass地址的映射。 But message AddressProto consists only of string fields while Address class has inner classes.但是消息AddressProto仅包含字符串字段,而Address类具有内部类。

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.到目前为止,我已经编写了这样一个映射器,但是由于消息结构和类之间的差异,我不知道如何正确地将字段从消息AddressProto映射到类Address并返回。

@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): proto 消息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 : java类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;
        }

    }
}

The solution turned out to be very simple.结果证明解决方案非常简单。 Even too much :) You just need to write your own implementation.甚至太多 :) 您只需要编写自己的实现即可。

@Mapper(
    config = MapstructConfig.class,
    unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface AddressProtoMapper {
  default Address mapToAddress(AddressProto address){
    return new Address(
        ...
    );
  }

  default AddressProto mapAddressToProto(Address address) {
    return new AddressProto(
        ...
    );
  }
}

我可以用 mapstruct 映射一个从 ArrayList 延伸的 class<object> ?<div id="text_translate"><p> 尝试将从 Arraylist 扩展的 Object 映射到具有列表的 Class 时遇到问题,我的代码是:</p><ol><li> 首先是 Class,它从 ArrayList 延伸:</li></ol><pre> public class ClassOne extends ArrayList&lt;ClassTwo&gt; {}</pre><ol start="2"><li> 我需要映射到:</li></ol><pre> public class ClassTarget { private String companyId; private List&lt;ObjectTarget&gt; fieldListTarget; }</pre><p> 当我声明映射器时,错误是:</p><pre> java: Can't generate mapping method from iterable type to non-iterable type.</pre><p> 我认为错误在extends ArrayList&lt;SomeObject&gt;我不知道如何使用这种类型的 object 映射字段。</p></div></object> - Can I mapping with mapstruct a class that extends from ArrayList<Object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 mapStruct 的内部不可变类 - Inner immutable class with mapStruct MapStruct:Object class的映射 - MapStruct: Mapping of Object class 使用内部类中“外部”类的方法 - Using methods from the “outer” class in inner classes 子类的 Mapstruct 映射不起作用 - Mapstruct mapping of child classes is not working 使用 MapStruct 的嵌套映射 - Nested Mapping using MapStruct 使用 Mapstruct 映射 DTO - Mapping DTO using Mapstruct Mapstruct - 映射从基础 class 扩展的 DTO 时编译失败 - Mapstruct - compilation failure when mapping DTOs that extend from base class 我可以用 mapstruct 映射一个从 ArrayList 延伸的 class<object> ?<div id="text_translate"><p> 尝试将从 Arraylist 扩展的 Object 映射到具有列表的 Class 时遇到问题,我的代码是:</p><ol><li> 首先是 Class,它从 ArrayList 延伸:</li></ol><pre> public class ClassOne extends ArrayList&lt;ClassTwo&gt; {}</pre><ol start="2"><li> 我需要映射到:</li></ol><pre> public class ClassTarget { private String companyId; private List&lt;ObjectTarget&gt; fieldListTarget; }</pre><p> 当我声明映射器时,错误是:</p><pre> java: Can't generate mapping method from iterable type to non-iterable type.</pre><p> 我认为错误在extends ArrayList&lt;SomeObject&gt;我不知道如何使用这种类型的 object 映射字段。</p></div></object> - Can I mapping with mapstruct a class that extends from ArrayList<Object>? 如何在 mapstruct 中使用来自不同类的另一个映射 - How can I use another mapping from different class in mapstruct MapStruct 映射到不可修改的 model class - MapStruct mapping to unmodifiable model class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM