Skip to main content

Configuration of Endpoints

Providing a custom javax.ws.rs.core.Application implementation is the only portable way to register root resource and provider classes and indicate what lifecycle model the individual resources follow. For example:

package server;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

public class BookApplication extends Application {

   @Override
   public Set<Class<?>> getClasses() {
      Set<Class<?>> classes = new HashSet<Class<?>>();
      classes.add(BookStore.class);
      return classes;
   }

   @Override 
   public Set<Object> getSingletons() {
      Set<Object> classes = new HashSet<Object>();
      classes.add(new SearchService());
      classes.add(new BookExceptionMapper());
      return classes;
   }
}

The BookApplication indicates to the runtime that BookStore root resource has the per-request lifecycle. The SearchService root resource and BookExceptionMapper provider are singletons. In CXF one can register JAX-RS Applications in web.xml using a CXFNonSpringJaxrsServlet:

<servlet>
   <servlet-name>CXFServlet</servlet-name>
   <display-name>CXF Servlet</display-name>
   <servlet-class>
      org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
   </servlet-class>
   <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>
         server.BookApplication	      
      </param-value>
   </init-param>
</servlet>

Spring users can configure the JAX-RS endpoints using one or more jaxrs:server declarations:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxrs="http://cxf.apache.org/jaxrs"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">

   <import resource="classpath:META-INF/cxf/cxf.xml"/>
   <import resource=
      "classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

   <bean class="org.apache.cxf.systest.jaxrs.BookStore" id="serviceBean"/>
   <bean class="org.apache.cxf.systest.jaxrs.provider.JAXBElementProvider"
      id="jaxbProvider">
      <!-- customize the default JAXBElementProvider 
           by setting some of its properties -->
   </bean>
  
   <jaxrs:server id="bookservice" address="/bookstore">
   
   <jaxrs:serviceBeans>
      <ref bean="serviceBean" />
   </jaxrs:serviceBeans>		   

   <jaxrs:providers>
      <ref bean="jaxbProvider"/>
   </jaxrs:providers>

   <jaxrs:features>
      <!-- Register CXF features such as FastInfoset or Logging -->
   </jaxrs:features>
      
   <jaxrs:inInterceptors>
      <!-- Register CXF in interceptors, example, reuse common in 
           interceptors between JAX-WS and JAX-RS endpoints -->
   </jaxrs:inInterceptors>
   
   <jaxrs:outInterceptors>
      <!-- Register CXF out interceptors, example, reuse common out 
           interceptors between JAX-WS and JAX-RS endpoints -->
      </jaxrs:outInterceptors>
   </jaxrs:server>
</beans>

A single JAX-RS endpoint is registered with a jaxrs:server declaration. This declaration may reference multiple root resource beans with jaxrs:serviceBeans and multiple providers using jaxrs:providers. Note a jaxrs:server/@address attribute. It allows for registering multiple jaxrs:server endpoints with all of them referencing the same service beans but using differently configured JAX-RS providers.

The jaxrs:server endpoints can register CXF features, in and out CXF interceptors.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!