如何使用 Jersey 获取完整的 REST 请求正文?
如何使用 Jersey 获取 POST
请求的完整 HTTP REST 请求正文?
在我们的例子中,数据将是 XML。大小从 1K 到 1MB 不等。
docs 似乎表明您应该使用 MessageBodyReader< /code> 但我看不到任何例子。
How can one get the full HTTP REST request body for a POST
request using Jersey?
In our case the data will be XML. Size would vary from 1K to 1MB.
The docs seem to indicate you should use MessageBodyReader
but I can't see any examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看来您必须在这里使用
MessageBodyReader
。下面是一个使用 jdom 的示例:将此类添加到 jersey 部署将处理的资源列表中(我认为通常通过 web.xml 配置)。然后,您可以在常规资源类之一中使用此阅读器,如下所示:
我尚未验证它是否完全按照键入的方式工作,但这就是它的要点。更多阅读内容:
It does seem you would have to use a
MessageBodyReader
here. Here's an example, using jdom:Add this class to the list of resources your jersey deployment will process (usually configured via web.xml, I think). You can then use this reader in one of your regular resource classes like this:
I haven't verified that this works exactly as typed, but that's the gist of it. More reading here:
事实证明您根本不需要做太多事情。
请参阅下文 - 参数
x
将包含完整的 HTTP 正文(在我们的例子中为 XML)。Turns out you don't have to do much at all.
See below - the parameter
x
will contain the full HTTP body (which is XML in our case).您可以使用 @Consumes 注释来获取完整的正文:
注意:不要忘记请求的“Content-Type:application/xml”标头。
You could use the @Consumes annotation to get the full body:
Note: Don't forget the "Content-Type: application/xml" header by the request.
使用以下代码尝试此操作:
尝试休息服务的 url 结束..../serviceX/doSomething
Try this using this single code:
The url for try rest services ends .... /serviceX/doSomething
由于您正在以 xml 格式传输数据,因此您还可以直接从/到 pojo 进行编组。
jersey 用户指南,我在这里复制:
带有 JAXB 注释的 POJO:
资源:
生成/使用的 xml:
Since you're transferring data in xml, you could also (un)marshal directly from/to pojos.
There's an example (and more info) in the jersey user guide, which I copy here:
POJO with JAXB annotations:
Resource:
The xml that gets produced/consumed: