为什么使用 JAX-RS / Jersey?
抱歉,这个问题听起来很愚蠢,但是在使用 Jersey 开发了一些 RESTful 服务之后,我问自己这个问题 - 如果 REST 只是一种架构,而不是像 SOAP 这样的协议,为什么我们需要像 JAX-RS 这样的规范?
实际上,我在 google 上搜索了诸如“servlet 和 HTTP 上的 RESTful 服务之间有什么区别”之类的问题,并总结了社区的答案,我得到:
- RESTful 服务开发(在 Jersey 上)是一种架构,它本质上使用 servlet。
- 像 Jersey 这样的 JAX-RS 兼容工具提供了 XML/JSON 数据的简单编组-解编,为开发人员提供了帮助。
- REST 帮助我们以比普通 servlet 高效得多的方式使用 GET/POST/PUT/DELETE。
根据这些答案,我想如果我编写一个使用 JAXB (用于处理自动序列化)的 servlet,并且我在 servlet 代码中有效地使用 GET/POST/PUT/DELETE,我不使用像 Jersey 这样的工具,并且因此JAX-RS。
我知道我的这个说法是非常错误的,请纠正我。
PS:这个疑问实际上是在我必须用 PHP 开发一些 RESTful 服务时出现的。在继续阅读一些 RESTful PHP 代码后,我意识到它们只是相同的旧 PHP 脚本,带有一些用于处理 XML/JSON 的辅助方法。
Sorry, this questions sounds silly, but after developing some of my RESTful services using Jersey, I asked myself the question -- If REST is just an architecture, and not a protocol like SOAP, why do we need a specification like JAX-RS?
I actually googled for questions like "What is the difference between servlets and RESTful services over HTTP" and to sum up the community answers, I got:
- RESTful service development (on Jersey) is an architecture, which inherently uses servlets.
- JAX-RS compliant tools like Jersey provide easy marshalling-unmarshalling of XML/JSON data, helping the developers.
- REST helps us use GET/POST/PUT/DELETE in a fashion that is far efficient than normal servlets.
According to these answers, I guess if I write a servlet which uses JAXB (for dealing with automatic serialization), and I efficiently use GET/POST/PUT/DELETE in my servlet code, I don't use a tool like Jersey, and hence JAX-RS.
I know I am terribly wrong passing this statement, please correct me.
PS: This doubt actually came in when I had to develop some RESTful services in PHP. After going on through some of the RESTful PHP codes, I realized they are just the same old PHP scripts, with some helper methods for handling XML/JSON.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简答
因为它使 RESTful 服务的开发变得更加容易。
长答案
JAX-RS 是一种标准,可以轻松创建可部署到任何 Java 应用程序服务器的 RESTful 服务:GlassFish、WebLogic、WebSphere、JBoss 等。JAX-
RS 是Java EE,当 JAX-RS 与其他 Java EE 技术一起使用时,创建 RESTful 服务变得更加容易:
示例 JAX-RS 服务
了解更多信息:
Short Answer
Because it makes the development of RESTful services easier.
Long Answer
JAX-RS is a standard that makes it easy to create a RESTful service that can be deployed to any Java application server: GlassFish, WebLogic, WebSphere, JBoss, etc.
JAX-RS is part of Java EE, and when JAX-RS is used with other Java EE technologies it becomes even easier to create your RESTful service:
Sample JAX-RS Service
For More Information:
不,事实并非如此。 REST 是一种可以使用 servlet 实现的架构风格,但本质上并不使用它们,也本质上与 Java 没有任何关系。
JAX-RS 是一种 JSR 规范,为 RESTful Web 服务定义了 Java API。
Jersey 是 JAX-RS 的具体实现。
至于是否使用 Jersey 还是尝试遵守 JAX-RS 规范,这取决于您。如果它能让您的工作更轻松,那就太好了!不然没人逼你。
No, it is not. REST is an architecture style which can be implemented using servlets, but does not inherently use them, nor inherently have anything to do with Java.
JAX-RS is a JSR Specification defining a Java API for RESTful Web Services.
Jersey is a specific implementation of JAX-RS.
As to whether to use Jersey or try to be compliant to the JAX-RS specification, that's sort of up to you. If it makes your work easier, great! If not no one's forcing you.