我正在尝试使用 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/