Jetty 和 Jersey:所有东西如何组合在一起?

use*_*917 2 java rest jetty jersey

我正在寻找使用带有嵌入式 Jetty 的 jersey 来为我们的服务实现 Web API。我看到 ServletContextHandler 、 ServletHolder 和所有这些类用于让 Jetty 了解 Jersey 处理程序的代码。我很想知道在幕后,比如当我们编译这段代码时会发生什么,码头实际上是如何发现球衣处理程序的。我知道,如果我开始阅读文档,我将能够弄清楚,但是,寻找一些涵盖此主题的快速链接。有没有这样的链接?

谢谢。

use*_*917 11

Jetty can act as a http server and also a servlet container who deals with the lifecycle of servlets (init, service, destroy) etc. A servlet is a java class that extends HttpServlet class and can override init, service, destroy methods etc. Once jetty receives a request whose URL matches with that of a servlet, it loads the servlet in memory (if not already there), calls service method, and keeps it in memory until it destroys it.

Jersey library has provided a standard way of writing RESTful APIs where classes are annotated with tags like say GET/POST etc and the URL. These classes are called resource classes. It has also provided a servlet whose name is ServletContainer to hook up with Jetty's servlet container, that intercepts Jetty's request to process servlet (like for any servlet request to jetty this is the one class, that receives the request first). What this servlet does is it examines the request, matches with the resource classes URL that it is informed about, and then transfers control to that method of that resource class (i think it uses reflection for this routing). Therefore, the resource classes are not servlet itself, but the ServletContainer class of jersey is the only servlet active in the system. The list of resource classes ServletContainer knows about is configured by this property called "com.sun.jersey.config.property.packages"
使用 Jersey 的好处是实现你的 REST API 是你使用标准的方式编写你的代码,如果将来需要,你可以部署到任何其他标准的 servlet 容器,比如 tomcat,...