2010-07-06 228 views
1

我正在嘗試使用REST爲客戶端/服務器實現協議緩衝區。 如果我需要以字節格式發送協議緩衝區請求,我仍然有點困惑?通過REST發送協議緩衝區

我的意思是,在我的客戶端代碼中,是否需要將對象序列化爲字節數組? 例如

protoRequest.build.toByteArray()

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

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

這是應該做的事情嗎?

感謝

大衛

回答

0

你可以編碼的SerializeToString使用Base64結果。

1

您可以將輸入流用於此目的。服務器端代碼將看起來像下面的代碼

@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

1

我已經寫了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));