简体   繁体   English

Jetty和Jersey:一切如何融合在一起?

[英]Jetty and Jersey: How does everything fits together?

I am looking for using jersey with embedded Jetty for implementing web APIs for our service. 我正在寻找使用嵌入式Jetty的球衣来实现我们服务的Web API。 I see code where ServletContextHandler , ServletHolder and all these classes are used to let Jetty know about the Jersey handlers. 我看到代码使用ServletContextHandler,ServletHolder和所有这些类来让Jetty了解Jersey处理程序。 I am interested to know under the hood, like what happens when we compile this code, how jetty actually discovers the jersey handlers. 我有兴趣知道引擎盖下的问题,比如当我们编译这段代码时会发生什么,jetty实际上是如何发现泽西处理程序的。 I know that if I start reading the documentation, I will be able to figure out, however, looking for some quick links that covers this topic. 我知道如果我开始阅读文档,我将能够弄清楚,但是,寻找一些涵盖这个主题的快速链接。 Is there any such link? 有这样的链接吗?

Thanks. 谢谢。

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. Jetty可以作为http服务器,也可以作为servlet容器来处理servlet的生命周期(init,service,destroy)等.servlet是一个扩展HttpServlet类的java类,可以覆盖init,service,destroy方法等。 jetty接收一个URL与servlet匹配的请求,它将servlet加载到内存中(如果还没有),调用服务方法,并将其保存在内存中,直到它销毁它为止。

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. Jersey库提供了一种编写RESTful API的标准方法,其中类使用诸如GET / POST等标记和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). 它还提供了一个servlet,其名称为ServletContainer,用于连接Jetty的servlet容器,它拦截了Jetty处理servlet的请求(就像任何对jetty的servlet请求一样,这是一个接收请求的类)。 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). 这个servlet做的是检查请求,与通知它的资源类URL匹配,然后将控制转移到该资源类的方法(我认为它使用反射进行此路由)。 Therefore, the resource classes are not servlet itself, but the ServletContainer class of jersey is the only servlet active in the system. 因此,资源类本身不是servlet,但是jersey的ServletContainer类是系统中唯一活动的servlet。 The list of resource classes ServletContainer knows about is configured by this property called "com.sun.jersey.config.property.packages" ServletContainer知道的资源类列表由此属性配置,名为“com.sun.jersey.config.property.packages”
The benefit of using Jersey is implementing your REST APIs is you are using standard way of writing your code that you can deploy to any other standard servlet container if needed in future like tomcat, ... 使用Jersey的好处是实现你的REST API你是使用标准的代码编写方式,如果将来需要你可以部署到任何其他标准servlet容器,如tomcat,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM