RESTful Java with JAX-RS by Bill Burke

RESTful Java with JAX-RS by Bill Burke

Author:Bill Burke [Bill Burke]
Language: eng
Format: epub, mobi
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449377519
Publisher: O'Reilly Media
Published: 2009-11-09T16:00:00+00:00


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. Although Java EE 6 has additional discovery options, the Application class is the only portable way of telling JAX-RS which web services (@Path annotated classes) as well as which MessageBodyReaders, MessageBodyWriters, and ContextResolvers (@Provider annotated classes) you want deployed. I first introduced you to the Application class back in Chapter 3:

package javax.ws.rs.core; import java.util.Collections; import java.util.Set; public abstract class Application { private static final Set<Object> emptySet = Collections.emptySet(); public abstract Set<Class<?>> getClasses(); public Set<Object> getSingletons() { 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-annotated classes. JAX-RS web service classes follow the per-request model mentioned Chapter 3. @Provider classes are instantiated by the JAX-RS container and registered once per application.

The getSingletons() method returns a list of preallocated JAX-RS web services and @Provider-annotated classes. 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.

Let’s look at a simple example of an Application class:

import javax.ws.rs.core.Application; public class ShoppingApplication extends Application { public ShoppingApplication() {} public Set<Class<?>> getClasses() { HashSet<Class<?>> set = new HashSet<Class<?>>(); set.add(CustomerResource.class); set.add(OrderResource.class); set.add(ProduceResource.class); return set; } public Set<Object> getSingletons() { JsonWriter json = new JsonWriter(); CreditCardResource service = new CreditCardResource(); HashSet<Object> set = new HashSet(); set.add(json); set.add(service); return set; } }

Here, we have a class ShoppingApplication that extends the Application class. The getClasses() method allocates a HashSet and populates it with @Path annotated classes and returns the set. The getSingletons() method allocates a MessageBodyWriter class named JsonWriter and an @Path annotated class CreditCardResource. It then creates a HashSet and adds these instances to it. This set is returned by the method.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.