Sending protocol buffers via REST

为君一笑 提交于 2019-12-10 10:13:54

问题


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


回答1:


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);



回答2:


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));



回答3:


You could encode the result of SerializeToString using base64.



来源:https://stackoverflow.com/questions/3187270/sending-protocol-buffers-via-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!