You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Introduction 

The JAX-RS 2.0 specification (JSR-339) mandates the support of CDI and Apache CXF starting from the version 3.0 introduces the initial support of this feature. As the starting point, the emphasis has been done on supporting embedded Jety 8/9 and Tomcat 7/8 containers as primary deployment (though other application servers will be supported in the future). 

Architecture and design 

At the moment, the integration of Apache CXF and CDI revolves around two key components, residing in new module called cxf-integration-cdi

 - CXFCdiServlet servlet

 - JAXRSCdiResourceExtension portable CDI extension

The fact of including cxf-integration-cdi as a dependency allows  JAXRSCdiResourceExtension  portable CDI extension to be discovered by CDI container. The  JAXRSCdiResourceExtension creates the instance of the Bus and registers it with BeanManager. From this point, the  Bus instance is a regular CDI bean (with @Application scope) available for injection. This instance of the  Bus is being injected into CXFCdiServlet servlet once it is initialized by servlet container.

Depending on the design, JAXRSCdiResourceExtension may use zero-based configuration approach or rely on particular JAX-RS Application instances.

Zero-based Configuration

If the Apache CXF application contains only one single instance of JAX-RS Application (annotated with @ApplicationPath) with no singletons and classes defined, the following rules are being applied by JAXRSCdiResourceExtension in order to configure and publish the configured JAX-RS resources:

 - the instance of the JAX-RS Application (annotated with @ApplicationPath) is being created and registered with BeanManager

 - all instances of the discovered JAX-RS providers (annotated with @Provider) are being created and registered with BeanManager

 - all instances of the discovered JAX-RS resources (annotated with @Path) are being created and registered with BeanManager

Lastly, the instance of the JAXRSServerFactoryBean is being created and configured with all service beans and providers discovered before. Additionally, the providers are enriched with the services for javax.ws.rs.ext.MessageBodyReader and javax.ws.rs.ext.MessageBodyWriter, loaded via ServiceLoader. From this moment, Apache CXF application is ready to serve the requests. The quick example is shown below.

The empty JAX-RS Application:

@ApplicationPath("/api")
public class BookStoreApplication extends Application {
}

And one JAX-RS resource:

@Path("/bookstore/")
public class BookStore {
    @Inject private BookStoreService service;
    
    @GET
    @Path("/books/{bookId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Book getBook(@PathParam("bookId") String id) {
        return service.get(id);
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • No labels