I am trying to implement protocol buffers for client/server using REST. I am still a bit confused if I need to send protocol buffers request in byte format?
I mean, in my client code, do I need to serialize object to byte array? For example
protoRequest.build.toByteArray()
And in the server, do I need to c
@POST
@Consumes("application/octet-stream")
public byte[] processProtoRequest(byte[] protoRequest) {
ProtoRequest.Builder request = ProtoRequest.newBuilder();
request.mergeFrom(protoRequest)
}
Is this the right thing to do?
Thanks
David
You can use input stream for this purpose. Server Side Code will be look like the below code
@POST
public Response processProtoRequest(@Context HttpServletRequest req) {
ProtoRequest protoRequestObj = ProtoRequest.parseFrom(req.getInputStream());
///process protoRequestObj and convert into byte arry and send to clinet
return Response.ok(protoRequestObj.toByteArray(),
MediaType.APPLICATION_OCTET_STREAM).status(200).build();
}
client side will look like this:
ProtoRequest protoRequestObj = ProtoRequest.newBuilder(). //protocol buffer object
setSessionId(id).
setName("l070020").
build();
DefaultHttpClinet httpClinet = new DefaultHttpClinet();
HttpPost request = new HttpPost("http://localhost:8080/maven.work/service/mainServices/protoRequest");
request.addHeader("accept","application/octet-stream");
request.setEntity(protoRequestObj.toByteArray());
HttpResponse response = httpClient.execute(request);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With