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
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.
Deep Learning with Python by François Chollet(12570)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9778)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7070)
Microservices with Go by Alexander Shuiskov(6832)
Practical Design Patterns for Java Developers by Miroslav Wengner(6750)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6696)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6097)
The Art of Crafting User Stories by The Art of Crafting User Stories(5624)
NetSuite for Consultants - Second Edition by Peter Ries(5561)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5367)
Kotlin in Action by Dmitry Jemerov(5062)
