开发者

Sending protocol buffers via REST

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("开发者_StackOverflow社区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);


I have written a Step by Step tutorial about how to produce/consume a protocol buffer stream in a web service, using Jersey as the client JAX-RS implementation. I hope it will help you. :)

Server side :

@GET
@Path("/{galaxy}")
@Consumes(MediaType.TEXT_HTML)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getInfo(@PathParam("galaxy") String galaxyName){

    if(StringUtils.equalsIgnoreCase("MilkyWay", StringUtils.remove(galaxyName, ' '))){

        // The following method would call the DTO Galaxy builders.
        Galaxy milkyWay = MilkyWayFactory.createGalaxy();

        // This is the important line for you where where the generated toByteArray() method takes responsibility of serializing the instance into a Protobuf format stream
        return Response.ok(milkyWay.toByteArray(),MediaType.APPLICATION_OCTET_STREAM).status(200).build();
    }

    return Response.status(Status.NOT_FOUND).build();
}

Client side :

String serverContext = "learning-protobuf3-ws-service";
String servicePath = "ws/universe/milkyway";
String serviceHost = "localhost";
Integer servicePort = 8080;

javax.ws.rs.client.Client client = javax.ws.rs.client.ClientBuilder.newClient();

javax.ws.rs.client.WebTarget target = client.target("http://"+serviceHost+":"+servicePort+"/"+serverContext)
                                            .path(servicePath);


InputStream galaxyByteString = target.request(MediaType.TEXT_HTML)
        .header("accept",MediaType.APPLICATION_OCTET_STREAM)
        .get(InputStream.class);

Galaxy galaxy = Galaxy.parseFrom(IOUtils.toByteArray(galaxyByteString));


You could encode the result of SerializeToString using base64.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜