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

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

问题描述

我从 JavaEE 6 读到 web.xml 是可选的.

如果没有 web.xml,我如何告诉应用服务器使用 Jersey 作为 JAX-RS 规范的实现?

解决方案

@AlexNevidomsky 在他的回答中写的是正确的,至于如何实现没有web.xml的app配置;您使用 @ApplicationPath 注释 应用程序 子类.

@ApplicationPath("/api")公共类 AppConfig 扩展应用程序 {}

有关部署选项的更多信息,请参阅Jersey 文档:第 4 章应用程序部署和运行时环境

或者更常见的是,以 Jersey 作为实现,我们会扩展 ResourceConfig(它扩展了 Application).

@ApplicationPath(api")公共类 AppConfig 扩展了 ResourceConfig {公共 AppConfig() {包(package.to.scan");}}

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

首先,并非所有 Java EE 服务器都使用 Jersey.实际上,我知道使用 Jersey 的只有 Glassfish 和 WebLogic.JBoss 使用 Resteasy.Tom EE 使用 CXF.WebSphere 使用 Apache Wink.我能想到的就这些了.<​​/p>

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

Servlet 3.0 引入了可插拔机制,它使用了一个 ServletContainerInitializer.它的工作原理是,当 Server/Servlet 容器启动时,它会使用名为 javax.servlet.ServletContainerInitializer 的文件扫描 META-INF/services 文件夹的 jar.该文件应包含一个或多个 ServletContainerInitializer 实现的完全限定名称.

这个接口只有一个方法

void onStartup(java.util.Set>c, ServletContext ctx)

Set 将是一个类列表,符合 @HandlesTypesServletContainerInitializer 实现的注解.如果您查看 Jersey 的实现

@HandlesTypes({ Path.class, Provider.class, Application.class, ApplicationPath.class })公共最终类 JerseyServletContainerInitializer实现 ServletContainerInitializer {

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

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

Resteasy 用途

@HandlesTypes({Application.class, Path.class, Provider.class})公共类 ResteasyServletInitializer 实现了 ServletContainerInitializer

我不会和别人打交道.

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

I have read that from JavaEE 6 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?

解决方案

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 {}

For more information on deployment options, see the Jersey Docs: Chapter 4. Application Deployment and Runtime Environments

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");
    }
}

So how is this implemented...

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.

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

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.

This interface has only one method

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

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 {

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 uses

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

I won't get into to others.

Some source you can look at...

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

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