Java Reference
In-Depth Information
Java EE services and configuration information. Let's look at how JAX-RS can take advant-
age of these features.
Basic Configuration
Any JAX-RS implementation, whether it sits within a JAX-RS-aware or Java EE container,
must support the
@Context
injection of the
javax.servlet.ServletContext
and
javax.servlet.ServletConfig
interfaces. Through these interfaces, you can get access to
configuration information expressed in the WAR's
web.xml
deployment descriptor. Let's take
this
web.xml
file, for example:
<?xml version="1.0"?>
<web-app>
<web-app>
<context-param>
<param-name>
max-customers-size
</param-name>
<param-value>
10
</param-value>
</context-param>
</web-app>
</web-app>
In this
web.xml
file, we want to define a default maximum dataset size for a JAX-RS-based
customer database that returns a collection of customers through XML. We do this by defin-
ing a
<context-param>
named
max-customers-size
and set the value to 10. We can get
access to this value within our JAX-RS service by injecting a reference to
ServletContext
with the
@Context
annotation. For example:
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
protected
protected
int
int
defaultPageSize
=
5
;
@Context
public
public
void
void
setServletContext
(
ServletContext context
) {
String size
=
context
.
getInitParameter
(
"max-customers-size"
);
iif
(
size
!=
null
null
) {
defaultPageSize
=
Integer
.
parseInt
(
size
);
}
}
@GET
@Produces
(
"application/xml"
)
public
public
String
getCustomerList
() {
...
use defaultPageSize to create
and
return
return
list of XML customers
...