JAXB 编组和解组
我正在尝试使用 Jersey 研究 REST 服务方法如何工作。
在创建和访问 REST 服务时,我提出了 2 个选项。 我有 6 个参数,它们都是字符串,
- 将数据作为长逗号分隔的字符串传递,并在服务器端将其拆分。
- 使用 JAXB 并进行编组和解组。
我可以理解第一个选项将是最快的,但有人知道它会比第二个选项快多少,这是一种安全有效的方法吗?
- 如果有人能提到更多可能的选项,那就太好了。
谢谢
I am trying to study how the REST service approach works using Jersey.
I have come up with 2 options when creating and accessing a REST service.
I have 6 parameters which are all string
- Pass the data as a long comma separated string and at server side split it.
- Use JAXB and do Marshalling and Unmarshalling.
I can understand that 1st option will be the fastest but does anyone knows how much fast it will be than the 2nd option and is it a safe and efficient way to do this.
- It will be nice if someone can mention any more options that are possible..
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要逗号分隔的字符串,则必须编写自己的 MessageBodyReader/Writter。此外,您还需要确保参数本身不包含逗号等。并不是说它会成为阻止程序 - 只需注意这一点。
您还可以使用 Jettison 进行低级 JSON 编组/解组 - 这也应该相当快。或者使用杰克逊。请参阅 Jersey 用户指南中的各种 JSON 映射选项。
为了完整起见,另一个选择可能是使用 Form (本质上是 String->List 的映射) - 如果您使用它,则不需要特殊的 MessageBodyReader/Writter - Jersey 将为您处理它。您只需要使用 @Produce/@Consume("application/www-form-urlencoded") 注释您的方法。
You'll have to write your own MessageBodyReader/Writter if you want the comma separated string. Also you'll need to make sure the parameter itself does not contain a comma, etc. Not that it would be a blocker - just noting that.
You can also use low-level JSON marshaling/unmarshaling using Jettison - that should also be pretty fast. Or use jackson. See various JSON mapping options in Jersey user-guide.
Just for completeness, another option might be to use Form (which is essentially a map of String->List) - if you use that, no need for a special MessageBodyReader/Writter - Jersey will handle it for you. You just need to annotate your methods with @Produce/@Consume("application/www-form-urlencoded").
注意:我是EclipseLink JAXB (MOXy) JAXB 2 (JSR-222) 专家组。
将 JAXB 实现与 Jersey 结合使用,您可以选择传递 XML 或 JSON 消息,这对于许多客户端来说很容易进行交互。为了未知的性能增益而发明自己的格式很可能是不必要的微观优化。
这是我使用 Jersey & 组合在一起的示例。 GlassFish 中的 MOXy:
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
Using a JAXB implementation with Jersey will give you the option of passing an XML or JSON message that will be easy for many clients to interact with. Inventing your own format for the sake of an unknown performance gain is most likely an unnecessary micro optimization.
Here is an example I put together using Jersey & MOXy in GlassFish: