1

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

Khaled Ahmed
  • 1,074
  • 8
  • 21

1 Answers1

1

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

Paplusc
  • 1,080
  • 1
  • 12
  • 24
  • @AnatoliiStepaniuk for some reason, after i integrate github.com/entur/mapstruct-spi-protobuf get an error saying error: Couldn't retrieve Mapper annotation – Mr.G Apr 04 '22 at 05:40