如何使用 Jersey JAX-RS 返回结果集?
我正在运行查询以从数据库返回州、城市和邮政编码。我如何让 JAX-RS (泽西岛) 像这样返回这个;
<State>
<City>
<Zip></Zip>
</City>
</State>
等等......
我是否需要构建一个与我的xml结构匹配的对象,然后将其传递给JAX-RS,或者JAX-RS可以根据结果集为我构建它吗?
I am running a query to return State, City, and Zip from my database. How do I get JAX-RS (Jersey) to return this like this;
<State>
<City>
<Zip></Zip>
</City>
</State>
and so on......
Do I need to build an object that matches my xml structure, then pass that to JAX-RS, or can JAX-RS build it for me based on the resultset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,Jersey 无法直接从 ResultSet 创建输出。那是因为它不知道数据应该如何表示。您必须编写一个类来保存结果。您可以在该类上使用 JAXB 注释来更好地控制 XML 的外观。
No, Jersey can't create the output directly from a ResultSet. That's because it has no idea how the data is supposed to be represented. You'll have to write a class to hold the results. You can use JAXB annotations on that class to provide greater control over how the XML looks.
您可以在 JAX-RS 服务中使用 POJO。然后使用 JPA 将对象映射到数据库,并使用 JAXB 将它们映射到 XML。有关示例,请参阅:
You could use POJOs in your JAX-RS service. Then use JPA to map the objects to the database, and JAXB to map them to XML. For an example see: