Servlet 与 RESTful
今天我读到了 Restful 服务。基本上我的理解是 Restful Web 服务将在 HTTP 请求方法上工作,而不是普通的 Web 服务将在 SOAP 请求上工作。
既然普通的 servlet 也可以在 HTTP 方法上工作,那么 Restful 服务需要什么?
Today I read about Restful services. Basically what I understand that is Restful webservices will work on HTTP request methods rather than normal webservice will work on SOAP request.
What is the need for Restful services as normal servlet can also work on the HTTP methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RESTful 与其说是一种不同的技术,不如说是一种架构风格。从服务器的角度来看,它被设计为完全无状态并且基于每个请求是独立的(即没有会话)。从客户端的角度来看,它更像是一种通过带有(自文档化)路径参数而不是请求参数的 URL 获取不同格式信息的方法。
当然,您可以使用普通的 servlet 来完成此操作,但它会引入一些样板代码来收集路径参数并生成所需的响应。 JAX-RS 只是一个方便且独立的 API,它消除了您自己编写所有样板代码的需要,从而产生最少且更多的自文档化代码。
假设您有一个如下所示的 JAXB 实体模型:
以及如下所示的 JAX-RS 资源:
那么您已经通过以下方式获得了正确格式的所需内容:
就是这样。尝试对单个普通 Servlet 执行相同的操作:) 请注意,SOAP 本质上也通过 HTTP 进行。它基本上是 HTTP 上的额外 XML 层,而不是不同的网络协议。
另请参阅:
RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it's more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters.
Surely you can do this with a plain vanilla servlet, but it would introduce some boilerplate code to gather the path parameters and to generate the desired response. JAX-RS is just a convenient and self-containing API which removes the need for writing all the boilerplate code yourself, resulting in minimal and more self-documenting code.
Assuming that you've a JAXB entity as model as below:
And a JAX-RS resource as below:
Then you'd already get the desired content in proper format by:
That's it. Try to do the same with a single plain vanilla Servlet :) Please note that SOAP essentially also goes over HTTP. It's basically an extra XML layer over HTTP, not a different network protocol.
See also:
在我看来,为了更好地理解,我们需要剖析那些让我们困惑的组件,这些组件是,
超文本传输协议
javax.servlet.http.HttpServlet
classJAX-RS 和 Spring Rest 之间的区别
那么如果您参考 这个答案了解这些实现方式使用Servlet(具体的
javax.servlet.http.HttpServlet
)来拦截所有传入请求。有重要的引述,你可以进一步阅读 - REST 和 REST 有什么区别和 HTTP 协议? & HTTP 和 REST 之间有什么区别? 并制作一个结论是,如果您将 Web 服务设为 RESTFul,您将获得哪些优势,即(从一个答案复制),
REST 不一定与 HTTP 相关。 RESTful Web 服务只是遵循 RESTful 架构的 Web 服务。
使用 REST 的优势是什么
不过,我不想陷入优缺点(优点和缺点)的争论,因为这是非常主观的。
读完了上面的内容,现在回答你的问题,
您会明白,REST 框架只是简化了企业级 REST 服务的实现,但它们确实使用 HTTP Servlet 来拦截传入请求。您始终可以使用普通 servlet 来实现您自己的 REST 服务,但如果使用大量样板代码,这只会更加耗时。
In my opinion, for a better understanding , we need to dissect components that confuse us and these components are ,
Hypertext Transfer Protocol
javax.servlet.http.HttpServlet
classDifference between JAX-RS and Spring Rest
Then if you refer this answer to understand as how these implementation use a Servlet ( A concrete
javax.servlet.http.HttpServlet
) to intercept all incoming requests. Important quote there is,Then you can further read about - What is the difference between REST and HTTP protocols? & What is the difference between HTTP and REST? and make a conclusion as what advantages you get if you make your web service RESTFul , namely ( copied from one answer ) ,
REST is not necessarily tied to HTTP. RESTful web services are just web services that follow a RESTful architecture.
What is the advantage of using REST instead of non-REST HTTP?
Though, I wouldn't like to get into advantages - disadvantages ( pros & cons ) debate as that is very subjective.
With above readings , now for your question ,
You would understand that REST Frameworks simply simplify implementation of REST Services at enterprise level but they do use HTTP Servlet to intercept incoming requests. You can always use plain servlets to implement your own REST services but that would simply be more time consuming with lots of boiler plate code.
RESTeasy 更容易进行 EJB 3.0 和 SEAM 集成,而 Jersey 更容易进行 Spring 和 JSON 集成.
RESTeasy is easier for EJB 3.0 and SEAM integration, whereas Jersey is easier for Spring and JSON integration.