如何在不使用web.xml的情况下将Jersey用作JAX-RS实现? [英] How to use Jersey as JAX-RS implementation without web.xml?

查看:158
本文介绍了如何在不使用web.xml的情况下将Jersey用作JAX-RS实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从java EE6中读到了web.xml是可选的。所以没有web.xml,我如何告诉应用服务器使用Jersey作为JAX-RS规范的实现?

I have read that from java EE6 web.xml is optional. So without web.xml, how can I tell the application server to use Jersey as the implementation for JAX-RS specification?

推荐答案

@AlexNevidomsky在他的回答中所写的内容是正确的,至于如何在没有web.xml的情况下实现app配置;你使用 @ApplicationPath < <$ href =https://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Application.html\"rel =nofollow上的/ code> 注释noreferrer> 应用程序 子类。

What @AlexNevidomsky wrote in his answer is correct, as far as how to implement the app configuration with no web.xml; you use an @ApplicationPath annotation on an Application subclass.

@ApplicationPath("/api")
public class AppConfig extends Application {}

有关部署的更多信息选项,请参阅 JAX-RS规范 - > 2.3出版物 - > 2.3。 2 Servlet

For more information on deployment options, see the JAX-RS spec -> 2.3 Publication -> 2.3.2 Servlet

或者更常见的是,使用Jersey作为实现,我们将扩展 ResourceConfig (扩展申请)。

Or more commonly, with Jersey as implementation, we would extend ResourceConfig (which extends Application).

@ApplicationPath("api")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("package.to.scan");
    }
}

那么这是如何实现的......

首先,并非所有Java EE服务器都使用Jersey。实际上我认识的唯一使用Jersey的是Glassfish和WebLogic。 JBoss使用Resteasy。 Tom EE使用CXF。 WebSphere使用Apache Wink。这是我能想到的唯一的。

First things first, not all Java EE servers use Jersey. Actually the only ones I know that use Jersey are Glassfish and WebLogic. JBoss uses Resteasy. Tom EE uses CXF. WebSphere uses Apache Wink. Those are the only ones I can think of.

所以我想问题是服务器如何知道如何加载JAX-RS应用程序?

So I guess the question is "How does the Server know how to load the JAX-RS application?"

Servlet 3.0引入了可插拔机制,该机制利用 ServletContainerInitializer 。它是如何工作的,当服务器/ Servlet容器启动时,它会扫描罐子中的 META-INF / services 文件夹,文件名为 javax。 servlet.ServletContainerInitializer 。此文件应包含 ServletContainerInitializer 的一个或多个完全限定的实现名称。

Servlet 3.0 introduced the pluggability mechanism, which makes use of a ServletContainerInitializer. How it works is that when the Server/Servlet container is started, it scans jars for a META-INF/services folder with a file named javax.servlet.ServletContainerInitializer. This file should include one or more fully qualified names of implementations of the ServletContainerInitializer.

此接口只有一个方法

void onStartup(java.util.Set<java.lang.Class<?>> c, ServletContext ctx)

设置< Class<?> 将是一个类列表,符合 @HandlesTypes 注释 ServletContainerInitializer 实施。如果你看一下Jersey的实现

The Set<Class<?> will be a list of classes, fitting the criteria in the @HandlesTypes annotation on the ServletContainerInitializer implementation. If you look at Jersey's implementation

@HandlesTypes({ Path.class, Provider.class, Application.class, ApplicationPath.class })
public final class JerseyServletContainerInitializer 
                   implements ServletContainerInitializer {

你应该注意到一些熟悉的注释类,以及 Application.class 。扫描时,所有符合条件的类都会添加到传递给 onStartup 方法的 Set 中。

You should notice some familiar annotation classes, as well as the Application.class. All these classes matching the criteria, while scanning, are added to the Set passed to the onStartup method.

如果您扫描其余的源代码,您将看到所有这些类的注册都已完成。

If you scan the rest of the source code, you will see all the registration being done with all of those classes.

Resteasy使用

Resteasy uses

@HandlesTypes({Application.class, Path.class, Provider.class})
public class ResteasyServletInitializer implements ServletContainerInitializer

我不会接触到其他人。

您可以查看一些来源......

  • JerseyServletContainerInitializer source code
  • ResteasyServletInitializer source code
  • JAX-RS specs

这篇关于如何在不使用web.xml的情况下将Jersey用作JAX-RS实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆