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

Compare with Current View Page History

« Previous Version 7 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. The org.apache.cxf.cdi.CXFCdiServlet should be configured as well (more examples for programmatic and WAR scenarios below).

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);
    }
}

Customized Configuration

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

  • the instance of each JAX-RS Application (annotated with @ApplicationPath) is being created and registered with BeanManager
  • for each JAX-RS Application instance created before, the instance of the JAXRSServerFactoryBean is being created and configured in a way that application's singletons/classes are being splitted to providers (annotated with @Provider), service beans (annotated with @Path) and features (implementation of org.apache.cxf.feature.Feature)

From this moment, Apache CXF application is ready to serve the requests. The quick example is shown below.

The configured JAX-RS Application:

@ApplicationPath("/custom")
public class BookStoreCustomApplication extends Application {
    @Inject private BookStore bookStore;
    
    @Override
    public Set< Object > getSingletons() {
        return Sets.< Object >newHashSet(
            bookStore, 
            new JacksonJsonProvider(),
            new ValidationExceptionMapper(),
            new JAXRSBeanValidationFeature());
    }
}

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);
    }
}

Deploying with embedded Jetty 8/9 (programmatic configuration)

TODO: in progress

Deploying with embedded Jetty 8/9 (WAR-based deployment) 

TODO : in progress

Deploying with embedded Tomcat 7/8 (WAR-based deployment) 

TODO : in progress

 

 

 

  • No labels