休息服务的应用程序类生命周期是什么? [英] What is that Application class lifecycle of a rest service?

查看:24
本文介绍了休息服务的应用程序类生命周期是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否每个休息服务都是从扩展该应用程序类和定义应用程序路径开始的?该应用程序类本身的生命周期是多少?下面是一个例子:

Is every rest service starting with extending that application class and defining applicationpath? What is the lifecyce of that application class itself? Here is an example:

import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends Application {}

这是一个servlet吗?它永远活着吗?我该如何理解这门课?是cdi豆吗?服务器是否在每次请求时都创建这个类?

Is this a servlet? Is it always alive? How shall I understand this class? Is it a cdi bean? Does the server creates this class on every request?

推荐答案

什么是Application?

应用程序 是 JAX-RS 提供的与部署无关的抽象类,用于配置和注册 JAX-RS 应用程序的组件,它还用于为应用程序提供额外的元数据.

What is Application?

Application is a deployment agnostic abstract class provided by JAX-RS for configuring and registering the components of a JAX-RS application and it's also used to supply additional metadata to the application.

应用程序 是可以使用 @Context 注释.有关详细信息,请参阅此答案.

应用程序 子类可以实现getClasses(), getSingletons()getProperties() 用于配置和注册组件和属性.

Application subclasses can implement methods such as getClasses(), getSingletons() and getProperties() for configuring and registering components and properties.

应用程序 子类可以用 @ 注释ApplicationPath,定义 JAX-RS 资源类的基本 URI(用 @Path).应用程序 子类在 Web 应用程序启动时实例化一次,并由 JAX-RS 运行时管理.

Application subclasses can be annotated with @ApplicationPath, defining the base URI for the JAX-RS resource classes (classes annotated with @Path). Application subclasses are instantied once when the web application starts and they are managed by the JAX-RS runtime.

最简单的实现方式如下:

The simplest implementation possible is as following:

@ApplicationPath("api")
public SampleApplication extends Application {

}

在上面的示例中,没有注册资源类或提供者,因此 JAX-RS 运行时将扫描 JAX-RS 组件的类路径并自动注册它们.

In the example above no resources classes or providers are registered, so the JAX-RS runtime will scan the classpath for JAX-RS components and will register them automatically.

但是,根据这篇来自 Jakub Podlesak 的帖子,在生产中不鼓励这种方法环境:

However, according to this post from Jakub Podlesak, this approach is discouraged in production environments:

上面的例子很好用.启动时,应用程序只扫描实际的类路径,并将在那里找到的每个 JAX-RS 组件类添加到实际的运行时配置中.是不是很棒?坦率地说,这种配置可以正常工作.直到有人更改系统配置(系统类路径)或应用程序的打包方式(然后可以从应用程序类路径中添加/删除新的第 3 方组件).这些更改可能超出您的控制,如果其中之一发生,您的应用程序配置可能会中断.因此,在生产环境中使用这种配置是不明智的.

The above example works great. When started, the application just scans the actual class-path, and adds every single JAX-RS component class found there to the actual runtime configuration. Isn't is great? Frankly, this kind of configuration could work just fine. Until someone changes either the system configuration (system class-path) or the way how you application is being packaged (a new 3rd party component could be added/removed from the application class-path then). These changes could be out of your control and if one of them happens, you application configuration could break. For this reason, it is not wise to use this kind of configuration in a production environment.

Jersey,JAX-RS 参考实现,提供了 ResourceConfig 类.与 Application<相比/a>, ResourceConfig 提供高级功能来简化 JAX-RS 组件的注册,例如扫描提供的类路径或一组包名称中的根资源和提供程序类等.有关详细信息,请参阅 泽西岛文档.

Jersey, the JAX-RS reference implementation, provides the ResourceConfig class. Compared to Application, ResourceConfig provides advanced capabilities to simplify registration of JAX-RS components, such as scanning for root resource and provider classes in a provided classpath or a in a set of package names, etc. For more details, refer to the Jersey documentation.

还值得一提的是,您不仅限于单个 Application 每个网络应用程序的子类.同一个 WAR 可以有多个 Application 子类.有关更多详细信息,请查看此 来自 Adam Bien 的帖子:

Is also worth mentioning that you are not restricted to a single Application subclass per web application. The same WAR can have multiple Application subclasses. For more details, have a look at this post from Adam Bien:

要在一个 WAR 中部署多个具有不同 URI 的 JAX-RS 应用程序,您必须为每个这样的应用程序创建一个 javax.ws.rs.core.Application 子类(或使用 web.xml 用于此目的).显然,Java EE 中普遍存在的关于配置(或按异常配置)的约定不再起作用:您必须通过覆盖方法 getClassesgetSingletons:

To deploy multiple JAX-RS applications with different URIs in one WAR you will have to create one javax.ws.rs.core.Application subclass per such an application (or use web.xml for this purpose). Obviously the in Java EE ubiquitous Convention over Configuration (or Configuration by Exception) cannot work any more: you will have to explicitly configure resources in each subclass by overriding the method getClasses or getSingletons:

@Path("first")
public class FirstResource {

    @GET
    public String first() {
        return "first";
    }
}

@ApplicationPath("one")
public class JAXRSConfigurationOne extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        resources.add(FirstResource.class);
        return resources;
    }
}

@Path("second")
public class SecondResource {

    @GET
    public String first() {
        return "second";
    }
}

@ApplicationPath("two")
public class JAXRSConfigurationTwo extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        resources.add(SecondResource.class);
        return resources;
    }
}

两个 JAX-RS 应用程序都可以通过不同的 URI 访问:http://localhost:8080/multiple-roots/one/firsthttp://localhost:8080/multiple-根数/两/秒

Both JAX-RS applications become accessible through distinct URIs: http://localhost:8080/multiple-roots/one/first and http://localhost:8080/multiple-roots/two/second

如果没有 Application 子类怎么办?

如果没有 Application 子类存在,JAX-RS 实现需要添加一个 servlet 并将其名称设置为 javax.ws.rs.Application 并自动发现所有资源类和提供者必须与应用程序一起打包.

What if no Application subclass is present?

If no Application subclass is present, the JAX-RS implementations are required to add a servlet and set its name to javax.ws.rs.Application and to automatically discover all resource classes and providers which must be packaged with the application.

有关更多详细信息,请查看 JAX-RS 2.1 规范.

For further details, have a look at the chapter 2 of the JAX-RS 2.1 specification.

这篇关于休息服务的应用程序类生命周期是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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