Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

protocol buffers - 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

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

Exception message was: cannot find a (map) key deserializer for type simple type

question from:https://stackoverflow.com/questions/65917697/how-to-map-between-simple-object-to-protobuff-object-in-java-using-object-mapper

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

My recomendation for this purpose is using mapstruct which provides a wide functionality to map java beans. Specially between protos and POJO's.

Just by creating an interface mapper for the class you want to map the framework generates the implementacion.

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 1.3.1.Final Reference Guide


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...