JAX-RS provides a deployment agnostic abstract class application for declaring root resource and provider classes , and Root resource and provider singleton instances. A Web Service may extend this class to declare root resource and provider classes. For example,
Public class extends Application { public set<class<?>> getclasses () { Setnew Hashset<class<?>>(); S.add (Helloworldresource. class ); return s; }}
Alternatively it is possible to reuse one of the Jersey ' s implementations that scans for root resource and provider classes given a classpath or a setof package names. Such classes is automatically added to the set of classes that is returned by getClasses
. For example, the following scans-root resource and provider classes in packages "Org.foo.rest", "Org.bar.rest" and in Any sub-packages of those:
Public class extends Packagesresourceconfig { public MyApplication () { super(" Org.foo.rest;org.bar.rest ");} }
There is multiple deployment options for the class, implements application interface in the Servlet 3.0 cont Ainer. For simple deployments, no web. XML is neededat all. Instead, an @ApplicationPath annotation can is used to annotate the user defined application class and specify th E The base resource URI of all application resources:
@ApplicationPath ("Resources")publicclassextends packagesresourceconfig { Public MyApplication () { super("Org.foo.rest;org.bar.rest"); } ...}
Another deployment option is to declare JAX-RS application details in the Web. This was usually suitable in case of more complex deployments, e.g. when security model needs to be properly defined or whe n Additional initialization parameters has the to is passed to Jersey runtime. Jax-rs 1.1 Specifies that a fully qualified name of the class that implements application is declared in the <servlet-name> element of the Jax-rs application ' s web. This is supported in a WEB container implementing Servlet 3.0 as follows:
<Web-app> <servlet> <Servlet-name>Org.foo.rest.MyApplication</Servlet-name> </servlet> ... <servlet-mapping> <Servlet-name>Org.foo.rest.MyApplication</Servlet-name> <Url-pattern>/resources</Url-pattern> </servlet-mapping> ...</Web-app>
Note that the <servlet-class> element was omitted from the servlet declaration. This is a correct declaration utilizing the Servlet 3.0 extension mechanism. Also Note that <servlet-mapping> was used to define the base resource URI.
When running in a servlets 2.x then instead it's necessary to declare the Jersey specific Servlet and pass th E Application implementation class name as one of the servlet ' s Init-param entries:
<Web-app> <servlet> <Servlet-name>Jersey WEB Application</Servlet-name> <Servlet-class>Com.sun.jersey.spi.container.servlet.ServletContainer</Servlet-class> <Init-param> <Param-name>Javax.ws.rs.Application</Param-name> <Param-value>Org.foo.rest.MyApplication</Param-value> </Init-param> ... </servlet> ...</Web-app>
Alternatively a simpler approach is to let Jersey choose the PackagesResourceConfig
implementation automatically by declaring the packages as follows:
<Web-app> <servlet> <Servlet-name>Jersey WEB Application</Servlet-name> <Servlet-class>Com.sun.jersey.spi.container.servlet.ServletContainer</Servlet-class> <Init-param> <Param-name>Com.sun.jersey.config.property.packages</Param-name> <Param-value>Org.foo.rest;org.bar.rest</Param-value> </Init-param> ... </servlet> ...</Web-app>
Jax-rs also provides the ability to obtain a container specific artifact from an application instance. For example, Jersey supports using Grizzly as follows:
Selectorthread st = Runtimedelegate.createendpoint (new MyApplication (), Selectorthread. Class);
Jersey also provides Grizzly helper classes to deploy the Servletthread instance at a base URL for in-process deployment.
The Jersey samples provide many examples of servlet-based and grizzly-in-process-based deployments.
Jersey (1.19.1)-Deploying a RESTful Web Service