rest - 通过 REST 发送 Protocol Buffer

标签 rest protocol-buffers jax-rs

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

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

protoRequest.build.toByteArray()

在服务器中,我需要 c

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

这是正确的做法吗?

谢谢

大卫

最佳答案

为此,您可以使用输入流。服务器端代码将类似于下面的代码

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

关于rest - 通过 REST 发送 Protocol Buffer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3187270/

相关文章:

Android - 如何在skydrive中实现REST API的REST GET功能

android - 如何使用 Retrofit 将包含图像的类从 Android 发送到 Java Restful Web 服务器

java - 使用 RestClusterClient 在 Flink 集群上运行已部署的作业

java - 谷歌 ProtoBuf : Limit size of message

java - 带反射的嵌套 Protocol Buffer

git - Azure DevOps git 策略配置 api 损坏了吗?

.net - Protobuf-net : ProtoInclude and compatibility 中的继承

rest - 将 Clojure 与基于注释的 REST 服务器一起使用

java - 记录 Jersey 的 JSON 请求和响应

java - GSON - JsonSyntaxException - 第 7 行第 4 列的预期名称