Use Java contexts and dependency injection with JAX-RS
Java API for RESTful Web Services (JAX-RS) root resources and providers can be used in a Java Contexts and Dependency Injection (JCDI) enabled web archive (WAR). Simply add a valid WEB-INF/beans.xml file to your WAR file and any JAX-RS root resources and providers that are valid JCDI beans can use JCDI functionality.
JAX-RS classes in a JCDI-enabled archive can have @javax.inject.Inject annotated injection points, participate in JCDI lifecycle scopes, raise JCDI events, and have method interceptors and decorators.
JAX-RS root resource and provider classes must have a valid JCDI managed bean constructor. In general, if your classes have a constructor that has a parameter annotated with the @javax.ws.rs.core.Context annotation or with any JAX-RS parameter annotation such as @javax.ws.rs.QueryParam, we must refactor your code to use JAX-RS-annotated bean properties or fields instead. We can add an @javax.annotation.PostConstruct annotated method to help construct your object.
Additionally, JAX-RS root resource and provider classes must have a JCDI specified scope. Scopes control the lifecycle of a JCDI managed bean. Root resource classes can have any valid scope such as @javax.enterprise.context.RequestScoped, which makes the JAX-RS root resource class behave the same as in a non-JCDI enabled application. The javax.ws.rs.core.Application subclasses and JAX-RS providers must have the @javax.enterprise.context.ApplicationScoped annotation.
We might want to use JCDI to more quickly and easily develop the application, as well as increase the testability of our code. JCDI enables modern programming techniques such as dependency injection, an event-based programming model, and a more aspect-oriented approach towards cross-cutting concerns. Developers can use aspect-oriented programming to cleanly add functionality that is not part of the core business logic to a method. For example, one cross-cutting concern is logging. Some developers need to log the entry and exit of every single time a certain business method is called in an application. Developers now can use JCDI functionality such as method interceptors and decorators to more cleanly add this functionality. Developers can use dependency injection to more easily insert their own mock implementations during unit testing. The decoupling of previously hard coded dependencies allows developers to more easily test individual components.
JCDI beans can inject EJBs and use @javax.annotation.Resource injections to inject Java EE resources. EJBs can also be JCDI beans. EJBs with annotations such as @javax.ejb.Stateless can also use JCDI dependency injection and other JCDI features. The EJBs keep their transactional and other EJB features.
Java Contexts and Dependency Injection (JCDI) is a new Java EE 6 feature. It can change the programming model to make applications easier to develop while increasing maintainability. JAX-RS developers can use JCDI features, such as @javax.inject.Inject support, in root resource and provider classes.
Tasks
- Configure the development environment.
- Before starting developing JAX-RS applications, we must set up the development environment by adding the JAX-RS libraries on the class path.
- Define the resources in JAX-RS web applications.
- Resources are the basic building block of a RESTful service. Resources can contain static or dynamically updated data. Examples of resources from an online book store application include a book, an order from a store, and a collection of users. By identifying the resources in the application, we can make the service more useful and easier to develop.
- Configure the JAX-RS application.
We can configure JAX-RS applications in multiple . To take advantage of the Java EE 6 functionality, we can use the annotation scanning capabilities. By using annotation scanning, we can omit a JAX-RS javax.ws.rs.core.Application subclass or have a minimally defined javax.ws.rs.core.Application subclass. Alternatively, we can specify the IBM JAX-RS servlet or filter to use the functionality available in the IBM JAX-RS servlet and filter.
Using one of the JAX-RS Version 1.1 configuration methods, we can omit a javax.ws.rs.core.Application subclass in the application or have a javax.ws.rs.core.Application subclass that returns an empty set of classes to inform the JAX-RS runtime environment to find and use all the JAX-RS classes in the application. We might want to use this method when we do not want to manually add every relevant JAX-RS class to a javax.ws.rs.core.Application subclass as you develop the application.
By specifying the specific IBM JAX-RS servlet and filter, we can take advantage of and ensure specific IBM JAX-RS behavior. For example, using the IBM JAX-RS filter can be helpful in developing a web application with a mix of JAX-RS resources and JSP files with the same URL patterns.
Even though there is a JAX-RS V1.1 configuration method that supports the use of an optional web.xml file, to specify security constraints or roles, or we want to take advantage of other features enabled using a web.xml file, specify the information in a web.xml file.
Choose one of the following three methods to configure the JAX-RS application:
- Configure JAX-RS applications using JAX-RS 1.1 methods
Use this method to use the annotation scanning capabilities or to use the JAX-RS 1.1 configuration methods. Use the annotation scanning capabilities to promote application portability, to minimize the amount of configuration code, or to dynamically modify the application without changes to the application code.
- Configure the web.xml file for JAX-RS servlets
Use this method to specify features enabled using servlet initialization parameters to change the behavior and ensure that we get the IBM JAX-RS servlet. When using servlets, we can define a servlet path in the web.xml file that is appended to the base URL.
- Configure the web.xml file for JAX-RS filters
Use this method to use the filter when we have JSPs, other servlets and filters, and JAX-RS resources with a mix of URL patterns. We can configure the web.xml file to define filters that indicate the possible URLs on which the filter can be invoked.
- Implementing JAX-RS resources with JCDI functionality. Use any of the JCDI features separately or with one another to make the applications easier to create, maintain, and test.
- Implement JAX-RS resources with dependency injection
Use this method to use dependency injection to separate the creation of new instances of Java types from the use of those Java types in the code. Dependency injection allows for more easily testable code, while also improving the ability to manage applications.
- Implement JAX-RS resources with different lifecycle scopes
Use this method to use lifecycle scopes to better manage the application code. Lifecycle scopes only apply to managed beans.
- Implement JAX-RS resources with decorators and method interceptors
Use this method to write decorators and interceptors for our JAX-RS resource types.
- Assemble JAX-RS web applications.
- After developing the Java class files for our JAX-RS web application and edit the web.xml file to enable the JAX-RS servlet, we are ready to assemble the application. Assemble the web application into a WAR package. We can assemble the WAR package into an EAR package if required.
- Deploy JAX-RS web applications.
- After assembling the JAX-RS web application, we need to deploy the web archive (WAR) package or the EAR package onto the application server.
We have enabled an enterprise bean so that JAX-RS resources with JCDI functionality are exposed for consumption.
Implement a JAX-RS resource with decorators and method interceptors Implement JAX-RS resources with dependency injection Implement JAX-RS resources with different lifecycle scopes Web services specifications and APIs EJB application specifications and APIs