通过REST发送协议缓冲区

问题描述:

我正在尝试使用REST为客户端/服务器实现协议缓冲区。 如果我需要以字节格式发送协议缓冲区请求,我仍然有点困惑?通过REST发送协议缓冲区

我的意思是,在我的客户端代码中,是否需要将对象序列化为字节数组? 例如

protoRequest.build.toByteArray()

而在服务器上,我需要到c

@POST 
    @Consumes("application/octet-stream") 
    public byte[] processProtoRequest(byte[] protoRequest) { 
    ProtoRequest.Builder request = ProtoRequest.newBuilder(); 
    request.mergeFrom(protoRequest) 
} 

这是应该做的事情吗?

感谢

大卫

你可以编码的SerializeToString使用Base64结果。

您可以将输入流用于此目的。服务器端代码将看起来像下面的代码

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

} 

客户端看起来就像这样:

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

你的博客链接做*不*解决要求在所有的问题,你没有披露它是你自己的博客文章,你链接到。我已经删除了这些链接。 – 2012-11-28 18:09:11

+0

我的博客包含通过Web服务向Google协议缓冲区发送和接收数据的基本设置细节。我在这个问题上做了两天的研发,然后我找到了解决方案,然后把它放到我的博客中。这个问题类似于我的博客的内容以及我为解决这个问题所需的更改,您可以通过实施它来检查您的自我。 – 2012-11-28 18:16:40

+1

问题是“这是我的代码,这是正确的做法吗?”你没有说“是”或“否”,你的博客文章甚至没有试图解决这个问题。它可能位于“类似”主题上,但不适合链接到您的博客。您的个人资料中有您的博客网址 - 这是人们可以看到并访问您的博客的地方。堆栈溢出不是为了帮助您宣传您的博客。 – 2012-11-28 18:19:06

我已经写了Step by Step tutorial有关如何在Web服务生产者/消费者的协议缓冲流,使用Jersey作为客户端JAX-RS实现。我希望它能帮助你。 :)

服务器端:

@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(); 
} 

客户端:

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