JAX-RS Components

Using Declarative Services on OSGI

Each JAX-RS root resource or provider component is to be registered as an OSGi service. Resource classes are POJOs. A provider class may implement one or more interfaces defined by JAX-RS. Therefore the least common denominator is the java.lang.Object class.

There may be other services registered under java.lang.Object that are not JAX-RS components. In order to simplify the wiring of JAX-RS components with a concrete JAX-RS Application we introduce the OSGi component boolean property javax.ws.rs.

Register a JAX-RS root resource or provider

A root resource or provider needs to be registered as JAX-RS singleton:

/**
 *
 * @scr.service interface="java.lang.Object"
 */
public class MyRootResource {

    /**
     * Denotes the name of the <code>javax.ws.rs</code> property.
     * @scr.property type="Boolean" value="true"
     */
    private static final String PKEY_JAVAX_WS_RS= "javax.ws.rs";

    ....
}

Accessing JAX-RS root resources and providers

The target services for a reference are constrained by the reference's interface name and target property. The reference name singleton is used according to the JAX-RS class javax.ws.rs.Application

/**
 * @scr.reference name="singletons"
 *     cardinality="0..n" policy="dynamic"
 *     interface="java.lang.Object"
 *     target="(javax.ws.rs=true)"
 *     bind="addSingleton" unbind="removeSingleton"
 */

It's probably required to declare the bundle as a singleton. The manifest file entry of a singleton bundle looks like:

Bundle-SymbolicName: org.trialox.MyWebApp;singleton:=true

The singleton directive indicates that the bundle can only have a single version resolved. A value of true indicates that the bundle is a singleton bundle. The default value is false. The Framework must resolve at most one bundle when multiple versions of a singleton bundle with the same symbolic name are installed. Singleton bundles do not affect the resolution of non-singleton bundles with the same symbolic name.

Labels

 
(None)