[英]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 JSON
到POJO'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
.特别是在
protos
和POJO'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.