Google 协议缓冲区和 servlet

发布于 2024-07-28 23:01:22 字数 300 浏览 3 评论 0原文

我想知道如何使用谷歌协议缓冲区来接受请求并将响应发送回客户端? 我正在考虑编写一个将接受请求的servlet。 以下思路是否是实现此目的的正确方法: 1. 拥有一个 .proto 文件,它是传入请求的消息定义。 2.编写一个servlet来接受该请求,执行各种任务,例如查询数据库 然后发送响应。 此响应是否需要单独的 .proto 消息定义以及构成响应的所有字段? 3. 客户端是否只需调用 servlet 的 doGet() 方法并传递请求,然后就应该返回一个响应作为 protobuff 对象?

任何建议或想法将不胜感激。

I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request.
Is the following trail of thought the correct way to implement this:
1. Have a .proto file which is the message definition for the incoming request.
2. Write a servlet which accepts this request, does various tasks like querying database
and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response?
3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?

Any suggestion or idea will be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-08-04 23:01:22

通常您需要一条请求消息和一条响应消息,是的。 您可能还需要一个方法名称来描述操作 - 这当然是内置 PB 服务的工作方式。

客户端不会调用 doGet() - 它会发出请求(可能是 POST 而不是 GET),并且您的 servlet 将接收它。

现在,理想情况下,您可以拥有一个通用的“ProtocolBufferServlet”,它可以通过将请求交给实现适当接口的服务来为请求提供服务。

我建议您查看协议缓冲区服务的文档以及Java 服务生成的代码了解更多信息。 您可以实现一个通过 servlet 运行的 RpcChannel,或者让客户端直接发出 HTTP post。 您可能会在服务器端使用某种依赖注入来告诉 servlet 正在实现该服务。

Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.

The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.

Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.

I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.

‖放下 2024-08-04 23:01:22

嗨,

我已经启动并运行了。 我最终将一个 http 请求作为帖子发布到我的 servlet。 我能够获取请求协议缓冲区,读取请求,进行一些处理,然后发回响应。 一旦我开始工作,它实际上非常简单。 我们使用 1.proto 文件来定义请求和响应消息结构。

HI,

I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文