REST Java web service using protobuf - Stack Overflow
You can see inside the post how we can create a JX-RS web service which is producing Google protocol Buffer in response. Source code is available at my blog
what I am doing is converting the protocol buffer object int byte array from server side and sending is array via service: Service code is blow , populating object and puting it in response.
UserDTO.User user = UserDTO.User.newBuilder(). //protocol buffer object setSessionId(id). setName("l070020"). build(); return Response.ok(user.toByteArray(),MediaType.APPLICATION_OCTET_STREAM).status(200).build();
Protocol buffer object has ability to parse populate the protocol buffer object via stream. So i am using by stream for data communication. On client side I am making connection to web service
HttpGet request = new HttpGet("http://localhost:8080/maven.work/service/mainServices/get_user"); request.addHeader("accept","application/octet-stream"); HttpResponse response = httpClient.execute(request);
Protocol buffer has build in method to parse stream,blow I ma parsing stream from the response
User user = User.parseFrom(response.getEntity().getContent());
Similarly you can send the protocol buffer object in the form of byte array to server and server can get it from HTTP Servlet Request stream and parse it same like the client is doing.
Read full article from REST Java web service using protobuf - Stack Overflow
No comments:
Post a Comment