简体   繁体   English

How to map between simple object to protobuff object in java using Object Mapper

[英]How to map between simple object to protobuff object in java using Object Mapper

I want to map between simple object to protobuff using object mapper when i tried this it cause an exception我想使用 object 映射器在简单的 object 到 protobuff 之间进行 map,当我尝试这个时它会导致异常

ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(enterprise, EnterpriseMessage.Enterprise.class);

Exception message was: cannot find a (map) key deserializer for type simple type异常消息是:找不到简单类型的(映射)密钥反序列化器

In my opinion objectmapper is not the best option to map protos since objectmapper is used to map JSON into POJO's and vice versa.在我看来, objectmapper不是 map 原型的最佳选择,因为objectmapper用于 map JSONPOJO's ,反之亦然。

My recomendation for this purpose is using mapstruct which provides a wide functionality to map java beans .为此,我的建议是使用mapstruct ,它为 map java beans提供了广泛的功能。 Specially between protos and POJO's .特别是在protosPOJO's之间。

Just by creating an interface mapper for the class you want to map the framework generates the implementacion.只需为您想要 map 的class创建一个interface映射器,框架就会生成实现。

I write you an example that you can follow.我给你写了一个你可以效仿的例子。

import org.mapstruct.Mapper;

@Mapper
public interface EnterpriseProtoMapper {
  EnterpriseMessage.Enterprise toProto(Enterprise enterprise);
}

For further information, you can check mapStruct 's documentation in this link:有关更多信息,您可以在此链接中查看mapStruct的文档:

MapStruct 1.3.1.Final Reference Guide MapStruct 1.3.1.Final 参考指南

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM