问题描述
有人可以向我解释一下JAX-RS提供程序是什么以及'@Provider'注释的作用是什么?我一直在阅读文档,但我无法得到它。
如果有资源类为传入请求提供服务,那么提供商会做什么?当我创建持久资源类(不是每个请求的那个)时,它们与单例资源类有什么不同?或者那些类也是提供者?
Could anyone explain to me what a JAX-RS Provider is and what ‘@Provider’ annotation does? I have been reading documentation but I cant get it.
If there are resource classes that service the incoming requests, what do Providers do? How are they different from singleton resource classes when I create a persistent resource class (the one that is not per-request)? Or are those classes also providers?
推荐答案
提供者只是一种扩展和自定义JAX-RS运行时的方法。您可以将它们视为(可能)改变运行时行为的插件,以实现一组(程序定义的)目标。
Providers are a simply a way of extending and customizing the JAX-RS runtime. You can think of them as plugins that (potentially) alter the behavior of the runtime, in order to accomplish a set of (program defined) goals.
提供商 不 与资源类相同,它们在概念上存在于资源类和JAX-RS实现之间。如果它有帮助,您可以将它们视为与设备驱动程序相同的视图(存在于用户和内核空间之间)。这是一个广泛的概括。
Providers are not the same as resources classes, they exist, conceptually, at a level in-between resources classes and the JAX-RS implementation. If it helps, you can think of them in the same light as device drivers (existing between user and kernel space). This is a broad generalization.
当前的JAX-RS规范定义了三类提供者。它们之间的共同点是所有提供者必须通过@Provider注释来标识,并遵循构造函数声明的某些规则。除此之外,不同的提供者类型可能有额外的注释,并将实现不同的接口。
There are three classes of providers defined by the current JAX-RS specification. The commonality between them is that all providers must be identified by the @Provider annotation and follow certain rules for constructor declaration. Apart from that, different provider types may have additional annotations, and will implement different interfaces.
实体提供商
这些提供程序控制数据表示(如XML,JSON,CSV)到其Java对象的映射。
These providers control the mapping of data representations (like XML, JSON, CSV) to their Java object equivalents.
上下文提供程序
这些提供程序控制资源可以通过@Context注释访问的上下文。
These providers control the context that resources can access via @Context annotations.
异常提供程序
这些提供程序控制Java异常到JAX-RS响应实例的映射。
These providers control the mapping of Java exceptions to a JAX-RS Response instance.
您的运行时将附带许多预定义的提供程序,这些提供程序将负责实现基本级别的功能(例如,用于映射到从XML,翻译最常见的例外等)。您也可以根据需要创建自己的提供者。
Your runtime will come with a number of predefined providers that will be responsible for implementing a base level of functionality (e.g for mapping to and from XML, translating the most common exceptions etc etc). You can also create your own providers as needed.
是阅读这些不同提供商类型及其作用的良好参考(见第4章。
The JAX-RS specification is a good reference for reading up on these different provider types and what they do (see Chapter 4).
这篇关于JAX-RS中的Provider是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!