5

RESTを使用してクライアント/サーバーのプロトコルバッファを実装しようとしています。プロトコルバッファリクエストをバイト形式で送信する必要がある場合、まだ少し混乱していますか?

つまり、クライアントコードでは、オブジェクトをバイト配列にシリアル化する必要がありますか?例えば

protoRequest.build.toByteArray()

そしてサーバーでは、私はcする必要がありますか

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

これは正しいことですか?

ありがとう

デビッド

4

3 に答える 3

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);
于 2012-11-28T18:03:02.397 に答える
0

SerializeToStringbase64を使用した結果をエンコードできます。

于 2010-10-07T00:20:59.527 に答える