Search code examples
javaspring-bootprotocol-buffersmapstruct

How to map values from protobuf object to java String with mapstruct


Let say I have the following protobuf file

message Body{
   map<string, string> message_body 1;
}

And what I want to do is use mapstruct to map this to a Java String. Something like this

@Mapping(target = "messageBody", source = "messageBody", qualifiedByName = "logBodyToMessageBody")
MessageBody logMessageToMessageBody(LogMessageProtoBuf.Body body);

And my MessageBody class looks like this

public class MessageBody implements Serializable {

@Column
private String messageBody

}

And lets say I use the following custom mapper below

  @Named("logBodyToMessageBody")
    public String logBodyToMessageBody(LogMessageProtoBuf.Body body) {
        return body.toString();

Each time I try to build the project, mapstruct is telling me that it can't map the fields and that I should use a custom mapper. I am using a custom mapper but it doesn't even get to the point where it either sees or it does not like the custom mapper. Could someone please help me implement the custom mapper where I can take the values from my proto file and convert it to its java equivalent above?


Solution

  • The reason why it is not working is because the logBodyToMessageBody method is between LogMessageProtoBuf.Body and String. However, in your mapper definition you are mapping the messageBody from the LogMessageProtoBuf.Body into MessageBody. The method you have is not applicable in this case.

    If you upgrade to 1.4 you will have a different message saying that there was no applicable method due to the missing qualifiedByName.