使用 JAXB 将 JSON 编组/解组为 Java 类
我使用 JAX-RS 和 JAXB 注释成功将 POJO 编组为 JSON。
问题是,当我尝试使用相同的方法来取消编组我的请求时,它不起作用。据我所见 文档 JAX-RS 可以自动将 application/json 字符串编组和解组回 java 类。
我是否需要为此创建自己的 MessageBodyReader,或者框架在不使用 Jackson 库的情况下支持此功能?
I am successfully marshaling a POJO into JSON using JAX-RS and JAXB annotations.
The problem is that when I am trying to use the same for un-marshalling my request it doesn’t work. As far as I can see in the documentation JAX-RS can automatically marshal and unmarshal application/json strings back to java classes.
Do I need to create my own MessageBodyReader for that, or this is supported by the framework without using Jackson libraries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经在 RESTEasy 中成功做到了。我将其设置为使用和生成 XML 和 JSON。这是一个请求处理程序:
ReqData 是一个 JavaBean,即它有一个默认构造函数,并且有编组器找到的 setter 和 getter。我在 ReqData 中没有任何特殊的 JSON 标记,但在 XML 编组器顶部有 @XmlRootElement(name="data") ,在设置器上有 @XmlElement 标记。
我使用单独的 bean 进行输入和输出,但据我所知,您可以使用相同的 bean。
客户端程序在请求的实体主体中发送 JSON 字符串,并将 Context-Type 和 Accept 标头都设置为“application/json”。
I have been doing it successfully in RESTEasy. I have it set up to consume and produce both XML and JSON. Here is a request handler:
ReqData is a JavaBean, i.e. it has a default constructor and it has setters and getters that the marshaller finds. I don't have any special JSON tags in ReqData, but I do have @XmlRootElement(name="data") at the top for the XML marshaller and @XmlElement tags on the setters.
I use separate beans for input and output, but as far as I know you can use the same bean.
The client program sends the JSON string in the entity-body of the request, and sets the Context-Type and Accept headers both to "application/json".
编组为 XML 很容易,但我花了一些时间才弄清楚如何编组为 JSON。找到解决方案后就很简单了。
Marshalling to XML is easy, but it took me a while to figure out how to marshall to JSON. Pretty simple after you find the solution though.
我一直在使用 Apache Wink,为此我需要使用 JSON 提供程序,例如 Jettison(一位同事一直在使用 Jackson)。我在此处写下了我采取的步骤
我的猜测是您也需要使用 JSON 提供程序。有理由不使用 Jackson 提供商吗?
I've been working with Apache Wink and for that I have needed to use a JSON provider, such as Jettison (a colleague has been using Jackson). I wrote up the steps I took here
My guess is that you too will need to to use a JSON provider. Is there a reason not to use a Jackson provider?