Google protocol buffers and servlets

心不动则不痛 提交于 2019-12-06 13:35:37

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1162117/google-protocol-buffers-and-servlets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!