Java Reference
In-Depth Information
You will need to define a
web.xml
file for your JAX-RS deployments. How JAX-RS is de-
ployed within a servlet container varies between JAX-RS-aware (like within Java EE applic-
ation servers or standalone Servlet 3.x containers like Tomcat) and older JAX-RS-unaware
servlet containers. Let's dive into these details.
The Application Class
Before looking at what we have to do to configure a
web.xml
file, we need to learn about the
javax.ws.rs.core.Application
class. The
Application
class is the only portable way of
telling JAX-RS which web services (
@Path
annotated classes) as well as which filters, inter-
ceptors,
MessageBodyReader
s,
MessageBodyWriter
s, and
ContextResolver
s (providers)
package
package
javax
.
ws
.
rs
.
core
;
import
import
java.util.Collections
java.util.Collections
;
import
import
java.util.Set
java.util.Set
;
public
public abstract
abstract class
class
Application
Application
{
private
private static
static final
final
Set
<
Object
>
emptySet
=
Collections
.
emptySet
();
public
public abstract
abstract
Set
<
Class
<?>>
getClasses
();
public
public
Set
<
Object
>
getSingletons
() {
return
return
emptySet
;
}
}
The
Application
class is very simple. All it does is list classes and objects that JAX-RS is
supposed to deploy. The
getClasses()
method returns a list of JAX-RS web service and
provider classes. JAX-RS web service classes follow the
per-request
model mentioned in
application.
The
getSingletons()
method returns a list of preallocated JAX-RS web services and pro-
viders. You, as the application programmer, are responsible for creating these objects. The
JAX-RS runtime will iterate through the list of objects and register them internally. When
these objects are registered, JAX-RS will also inject values for
@Context
annotated fields
and setter methods.