JAX-RS 将实体获取为 JAXB 对象和字符串
我有一个 JAX-RS Web 服务(使用球衣),它接受 JAXB 对象作为请求实体。当我们收到错误时,我们想要记录发送给我们的原始 xml 字符串。目前,我只是重新编组 JAXB 对象,但由于这些类中有几个 java 枚举,原始 xml 字符串中拼写不正确的枚举值会丢失,这对于我们的目的来说是不可接受的。
有谁知道如何以字符串和 JABX 对象的形式获取请求实体?我不想编写自定义 MessageBodyReader,并且如果可能的话,我不想尝试获取 JAXB 的 MessageBodyReader。您也可以自由使用特定于球衣的课程。我们使用的是 1.0.x 版本。
I have a JAX-RS web service (using jersey) that accepts a JAXB object as the request entity. When we get an error, we want to log the original xml string that was sent to us. Currently, I am just re-marshalling the JAXB object, but since we have several java enums in those classes, enum values that are not spelled correctly in the original xml string are lost, which is not acceptable for our purposes.
Does anyone know a way to get the request entity as both a string and JABX object? I would prefer not to write a custom MessageBodyReader and I would prefer to not try and get the MessageBodyReader for the JAXB if possible. You are free to use jersey-specific classes as well. We are using version 1.0.x.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,使用 JAX-RS API 来做到这一点并不难。这就是我所做的:
只需几行代码即可将 xml 作为字符串和 JAXB 对象提供给您。
Turns out it isn't that hard to do this with the JAX-RS API. Here's what I did:
This will give you the xml as a string and as a JAXB object in just a few lines of code.
作为一个想法,您可以为 Web 应用程序添加一个 servlet 过滤器,该过滤器将拦截所有请求并将有效负载捕获到线程上下文中,以便稍后在需要时将其提取。
As an idea, you can add a servlet filter for your web application that would intercept all requests and capture payload into a thread context where it can be extracted from later on if needed.