Lilli
Lilli

Reputation: 101

ObjectMapper exception using gRPC

I'm using gRPC (http://www.grpc.io) and I have a request from the client to the server. I wish to use ObjectMapper mapper or Jackson in order to create a Json String.

For exameple

ObjectMapper mapper = new ObjectMapper();       

try {
    NFFGSrpcreq1=NFFGSrpc.newBuilder().addNffg(request).build();

    // Convert object to JSON string
    String jsonInString = mapper.writeValueAsString(req1);
    System.out.println(jsonInString);
} catch (JsonGenerationException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

But during the execution I have a exception comes launch.

Upvotes: 2

Views: 800

Answers (1)

Anar Sultanov
Anar Sultanov

Reputation: 3346

How important is it to use Jackson?

If this is important, then I will advise you to create a class for an intermediate object that you will create from NFFGSrpc req1 and then serialize it to JSON.

Otherwise you could use Protocol Buffers [Util] which provides JsonFormat.Printer class for converting protobuf messages to JSON format, e.g.:

JsonFormat.Printer printer = JsonFormat.printer();
NFFGSrpc req1 = NFFGSrpc.newBuilder().addNffg(request).build();
String jsonInString = printer.print(req);

Upvotes: 1

Related Questions