O'Reilly logo

RESTful Java with JAX-RS by Bill Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Configuration

All the examples in this book so far have been simple and pretty self-contained. Your RESTful web services will probably need to sit in front of a database and interact with other local and remote services. Your services will also need configuration settings that are described outside of code. I don’t want to get into too much detail, but the servlet and Java EE specifications provide annotations and XML configuration that allow you to get access to various Java EE services and configuration information. Let’s look at how JAX-RS can take advantage of these features.

Older Java EE Containers

Any JAX-RS implementation, whether it sits within a JAX-RS-aware or -unaware servlet 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>
   <context-param>
      <param-name>max-customers-size</param-name>
      <param-value>10</param-value>
   </context-param> <servlet> <servlet-name>JAXRS</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> com.restfully.shop.services.ShoppingApplication </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Rest</servlet-name> <url-pattern>/*</url-pattern> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required