cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF Documentation > JAXRS Services Configuration
Date Fri, 17 Jun 2011 09:59:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CXF20DOC&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Services+Configuration">JAXRS Services Configuration</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~sergey_beryozkin">Sergey Beryozkin</a>
    </h4>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>  &lt;import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; /&gt; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">  &lt;import resource=&quot;classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml&quot; /&gt; <br></td></tr>
            <tr><td class="diff-unchanged" >  &lt;import resource=&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot; /&gt; <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h1. Configuring JAX-RS services using explicit bean configuration <br> <br>Note that jaxrs:server (and jaxrs:client) declarations depend on &#39;http://cxf.apache.org/jaxrs&#39; Spring NamespaceHandler be available on classpath. Sometimes, due to classloading restrictions or bugs in underlying containers which are exposed during complex deployments or due to multiple Spring libraries interfering with each other, NamespaceHandler can not be located and thus jaxrs endpoints can not be created.  <br>Please report such issues to the team working on developing the container itself. <br> <br>If you need to do Spring configuration and get an error to do with a missing NamespaceHandler then, as a workaround, consider configuring jaxrs endpoints using CXF beans which actually handle the creation of jaxrs:server endpoints. This is marginally more complex, but overall, the configuration ends up being quite similar, for example, the above jaxrs:server endpoint can be configured like this instead: <br> <br>{code:xml} <br>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; <br>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; <br>  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; <br>  xsi:schemaLocation=&quot; <br>http://www.springframework.org/schema/beans <br>http://www.springframework.org/schema/beans/spring-beans.xsd&quot;&gt; <br> <br>  &lt;import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; /&gt; <br>  &lt;import resource=&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot; /&gt; <br> <br>  &lt;bean class=&quot;org.apache.cxf.jaxrs.JAXRSServerFactoryBean&quot; init-method=&quot;create&quot;&gt; <br>    &lt;property name=&quot;address&quot; value=&quot;/service1&quot;/&gt; <br>    &lt;property:serviceBeans&gt; <br>      &lt;ref bean=&quot;customerBean&quot; /&gt; <br>    &lt;/property:serviceBeans&gt; <br>  &lt;/jaxrs:server&gt; <br> <br>  &lt;bean id=&quot;customerBean&quot; class=&quot;demo.jaxrs.server.CustomerService&quot; /&gt; <br>&lt;/beans&gt; <br> <br>{code} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. Configuring JAX-RS services in container without Spring <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p><span style="font-size:2em;font-weight:bold"> JAX-RS : Services Configuration </span></p>

<div>
<ul>
    <li><a href='#JAXRSServicesConfiguration-ConfiguringJAXRSservicesprogrammatically'>Configuring JAX-RS services programmatically</a></li>
    <li><a href='#JAXRSServicesConfiguration-ConfiguringJAXRSendpointsprogrammaticallywithoutSpring'>Configuring JAX-RS endpoints programmatically without Spring</a></li>
    <li><a href='#JAXRSServicesConfiguration-ConfiguringJAXRSservicesincontainerwithSpringconfigurationfile.'>Configuring JAX-RS services in container with Spring configuration file.</a></li>
<ul>
    <li><a href='#JAXRSServicesConfiguration-web.xml'>web.xml</a></li>
<ul>
    <li><a href='#JAXRSServicesConfiguration-UsingSpringContextLoaderListener'>Using Spring ContextLoaderListener</a></li>
    <li><a href='#JAXRSServicesConfiguration-UsingCXFServletinitparameters'>Using CXFServlet init parameters</a></li>
</ul>
    <li><a href='#JAXRSServicesConfiguration-beans.xml'>beans.xml</a></li>
</ul>
    <li><a href='#JAXRSServicesConfiguration-ConfiguringJAXRSservicesusingexplicitbeanconfiguration'>Configuring JAX-RS services using explicit bean configuration</a></li>
    <li><a href='#JAXRSServicesConfiguration-ConfiguringJAXRSservicesincontainerwithoutSpring'>Configuring JAX-RS services in container without Spring</a></li>
<ul>
    <li><a href='#JAXRSServicesConfiguration-AttachingJAXRSendpointstoanexistingJettyserver'>Attaching JAXRS endpoints to an existing Jetty server</a></li>
</ul>
    <li><a href='#JAXRSServicesConfiguration-JAXRSRuntimeDelegateandApplications'>JAX-RS RuntimeDelegate and Applications</a></li>
    <li><a href='#JAXRSServicesConfiguration-ConfiguringJAXRSservicesprogrammaticallywithSpringconfigurationfile.'>Configuring JAX-RS services programmatically with Spring configuration file.</a></li>
    <li><a href='#JAXRSServicesConfiguration-Lifecyclemanagement'>Lifecycle management</a></li>
<ul>
    <li><a href='#JAXRSServicesConfiguration-FromSpring'>From Spring</a></li>
    <li><a href='#JAXRSServicesConfiguration-WithCXFNonSpringJaxrsServlet'>With CXFNonSpringJaxrsServlet</a></li>
    <li><a href='#JAXRSServicesConfiguration-Programmatically'>Programmatically</a></li>
    <li><a href='#JAXRSServicesConfiguration-PostConstructandPreDestroy'>PostConstruct and PreDestroy</a></li>
</ul>
    <li><a href='#JAXRSServicesConfiguration-Locatingcustomresourcesinwebapplications'>Locating custom resources in web applications</a></li>
    <li><a href='#JAXRSServicesConfiguration-Multipleendpointsandresourceclasses'>Multiple endpoints and resource classes</a></li>
    <li><a href='#JAXRSServicesConfiguration-DynamicservletsandasingleJAXRSendpoint'>Dynamic servlets and a single JAX-RS endpoint</a></li>
</ul></div>

<h1><a name="JAXRSServicesConfiguration-ConfiguringJAXRSservicesprogrammatically"></a>Configuring JAX-RS services programmatically</h1>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
...

JAXRSServerFactoryBean sf = <span class="code-keyword">new</span> JAXRSServerFactoryBean();
sf.setResourceClasses(CustomerService.class);
sf.setAddress(<span class="code-quote">"http:<span class="code-comment">//localhost:9000/"</span>);
</span>sf.create();
</pre>
</div></div>

<p>Some things to note:</p>
<ul>
	<li>The JAXRSServerFactoryBean creates a Server inside CXF which starts listening for requests on the URL specified.</li>
	<li>Check the <a href="http://cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.html" class="external-link" rel="nofollow">JAXRSServerFactoryBean API</a> for methods for adding multiple root resources</li>
	<li>setResourceClasses() is for root resources only, use setProvider() or setProviders() for @Provider-annotated classes.</li>
	<li>By default, the JAX-RS runtime is responsible for the lifecycle of resource classes, default lifecycle is per-request. You can set the lifecycle to singleton by using following line:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sf.setResourceProvider(BookStore.class, <span class="code-keyword">new</span> SingletonResourceProvider(<span class="code-keyword">new</span> BookStore()));
</pre>
</div></div></li>
	<li>If you prefer not to let the JAX-RS runtime handle the resource class lifecycle for you (for example, it might be the case that your resource class is created by other containers such as Spring), you can do the following:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JAXRSServerFactoryBean sf = <span class="code-keyword">new</span> JAXRSServerFactoryBean();
CustomerService cs = <span class="code-keyword">new</span> CustomerService();
sf.setServiceBeans(cs);
sf.setAddress(<span class="code-quote">"http:<span class="code-comment">//localhost:9080/"</span>);
</span>sf.create();
</pre>
</div></div></li>
</ul>



<h1><a name="JAXRSServicesConfiguration-ConfiguringJAXRSendpointsprogrammaticallywithoutSpring"></a>Configuring JAX-RS endpoints programmatically without Spring</h1>

<p>Note that even though no Spring is explicitly used in the previous section, it is still used by default to have various CXF components registered with the bus such as transport factories. If no Spring libraries are available on the classpath then please follow the following example :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JAXRSServerFactoryBean sf = <span class="code-keyword">new</span> JAXRSServerFactoryBean();
sf.setResourceClasses(CustomerService.class);
sf.setResourceProvider(CustomerService.class, <span class="code-keyword">new</span> SingletonResourceProvider(<span class="code-keyword">new</span> CustomerService()));
sf.setAddress(<span class="code-quote">"http:<span class="code-comment">//localhost:9000/"</span>);
</span>BindingFactoryManager manager = sf.getBus().getExtension(BindingFactoryManager.class);
JAXRSBindingFactory factory = <span class="code-keyword">new</span> JAXRSBindingFactory();
factory.setBus(sf.getBus());
manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
sf.create();
</pre>
</div></div> 

<h1><a name="JAXRSServicesConfiguration-ConfiguringJAXRSservicesincontainerwithSpringconfigurationfile."></a>Configuring JAX-RS services in container with Spring configuration file.</h1>

<h2><a name="JAXRSServicesConfiguration-web.xml"></a>web.xml</h2>

<p>In web.xml one needs to register one or more CXFServlet(s) and link to an application context configuration.</p>

<h3><a name="JAXRSServicesConfiguration-UsingSpringContextLoaderListener"></a>Using Spring ContextLoaderListener</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span> encoding=<span class="code-quote">"ISO-8859-1"</span>?&gt;</span>

&lt;!DOCTYPE web-app
    PUBLIC <span class="code-quote">"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"</span>
    <span class="code-quote">"http://java.sun.com/dtd/web-app_2_3.dtd"</span>&gt;
<span class="code-tag">&lt;web-app&gt;</span>
	<span class="code-tag">&lt;context-param&gt;</span>
		<span class="code-tag">&lt;param-name&gt;</span>contextConfigLocation<span class="code-tag">&lt;/param-name&gt;</span>
		<span class="code-tag">&lt;param-value&gt;</span>WEB-INF/beans.xml<span class="code-tag">&lt;/param-value&gt;</span>
	<span class="code-tag">&lt;/context-param&gt;</span>

	<span class="code-tag">&lt;listener&gt;</span>
		<span class="code-tag">&lt;listener-class&gt;</span>
			org.springframework.web.context.ContextLoaderListener
		<span class="code-tag">&lt;/listener-class&gt;</span>
	<span class="code-tag">&lt;/listener&gt;</span>

	<span class="code-tag">&lt;servlet&gt;</span>
		<span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet<span class="code-tag">&lt;/servlet-name&gt;</span>
		<span class="code-tag">&lt;display-name&gt;</span>CXF Servlet<span class="code-tag">&lt;/display-name&gt;</span>
		<span class="code-tag">&lt;servlet-class&gt;</span>
			org.apache.cxf.transport.servlet.CXFServlet
		<span class="code-tag">&lt;/servlet-class&gt;</span>
		<span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
	<span class="code-tag">&lt;/servlet&gt;</span>

	<span class="code-tag">&lt;servlet-mapping&gt;</span>
		<span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet<span class="code-tag">&lt;/servlet-name&gt;</span>
		<span class="code-tag">&lt;url-pattern&gt;</span>/*<span class="code-tag">&lt;/url-pattern&gt;</span>
	<span class="code-tag">&lt;/servlet-mapping&gt;</span>
<span class="code-tag">&lt;/web-app&gt;</span>
</pre>
</div></div>

<p>The application context configuration is shared between all the CXFServlets</p>

<h3><a name="JAXRSServicesConfiguration-UsingCXFServletinitparameters"></a>Using CXFServlet init parameters </h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span> encoding=<span class="code-quote">"ISO-8859-1"</span>?&gt;</span>

&lt;!DOCTYPE web-app
    PUBLIC <span class="code-quote">"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"</span>
    <span class="code-quote">"http://java.sun.com/dtd/web-app_2_3.dtd"</span>&gt;
<span class="code-tag">&lt;web-app&gt;</span>
	<span class="code-tag">&lt;servlet&gt;</span>
		<span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet1<span class="code-tag">&lt;/servlet-name&gt;</span>
		<span class="code-tag">&lt;display-name&gt;</span>CXF Servlet1<span class="code-tag">&lt;/display-name&gt;</span>
		<span class="code-tag">&lt;servlet-class&gt;</span>
			org.apache.cxf.transport.servlet.CXFServlet
		<span class="code-tag">&lt;/servlet-class&gt;</span>
                <span class="code-tag">&lt;init-param&gt;</span>
                   <span class="code-tag">&lt;param-name&gt;</span>config-location<span class="code-tag">&lt;/param-name&gt;</span>
                   <span class="code-tag">&lt;param-value&gt;</span>/WEB-INF/beans1.xml<span class="code-tag">&lt;/param-value&gt;</span>
                <span class="code-tag">&lt;/init-param&gt;</span>
		<span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
	<span class="code-tag">&lt;/servlet&gt;</span>

        <span class="code-tag">&lt;servlet&gt;</span>
		<span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet2<span class="code-tag">&lt;/servlet-name&gt;</span>
		<span class="code-tag">&lt;display-name&gt;</span>CXF Servlet2<span class="code-tag">&lt;/display-name&gt;</span>
		<span class="code-tag">&lt;servlet-class&gt;</span>
			org.apache.cxf.transport.servlet.CXFServlet
		<span class="code-tag">&lt;/servlet-class&gt;</span>
                <span class="code-tag">&lt;init-param&gt;</span>
                   <span class="code-tag">&lt;param-name&gt;</span>config-location<span class="code-tag">&lt;/param-name&gt;</span>
                   <span class="code-tag">&lt;param-value&gt;</span>/WEB-INF/beans2.xml<span class="code-tag">&lt;/param-value&gt;</span>
                <span class="code-tag">&lt;/init-param&gt;</span>
		<span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
	<span class="code-tag">&lt;/servlet&gt;</span>


	<span class="code-tag">&lt;servlet-mapping&gt;</span>
		<span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet1<span class="code-tag">&lt;/servlet-name&gt;</span>
		<span class="code-tag">&lt;url-pattern&gt;</span>/1/*<span class="code-tag">&lt;/url-pattern&gt;</span>
	<span class="code-tag">&lt;/servlet-mapping&gt;</span>

        <span class="code-tag">&lt;servlet-mapping&gt;</span>
		<span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet2<span class="code-tag">&lt;/servlet-name&gt;</span>
		<span class="code-tag">&lt;url-pattern&gt;</span>/2/*<span class="code-tag">&lt;/url-pattern&gt;</span>
	<span class="code-tag">&lt;/servlet-mapping&gt;</span>
<span class="code-tag">&lt;/web-app&gt;</span>
</pre>
</div></div>

<p>Each CXFServlet can get a unique application context configuration. Note, no Spring ContextLoaderListener is registered in web.xml in this case.</p>

<h2><a name="JAXRSServicesConfiguration-beans.xml"></a>beans.xml</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span> encoding=<span class="code-quote">"UTF-8"</span>?&gt;</span>
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
  <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
  <span class="code-keyword">xmlns:jaxrs</span>=<span class="code-quote">"http://cxf.apache.org/jaxrs"</span>
  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"&gt;

  <span class="code-tag"><span class="code-comment">&lt;!-- do not use import statements if CXFServlet init parameters link to this beans.xml --&gt;</span></span> 

  <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf.xml"</span> /&gt;</span>
  <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-servlet.xml"</span> /&gt;</span>

  <span class="code-tag">&lt;jaxrs:server id=<span class="code-quote">"customerService"</span> address=<span class="code-quote">"/service1"</span>&gt;</span>
    <span class="code-tag">&lt;jaxrs:serviceBeans&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"customerBean"</span> /&gt;</span>
    <span class="code-tag">&lt;/jaxrs:serviceBeans&gt;</span>
  <span class="code-tag">&lt;/jaxrs:server&gt;</span>

  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerService"</span> /&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<h1><a name="JAXRSServicesConfiguration-ConfiguringJAXRSservicesusingexplicitbeanconfiguration"></a>Configuring JAX-RS services using explicit bean configuration</h1>

<p>Note that jaxrs:server (and jaxrs:client) declarations depend on 'http://cxf.apache.org/jaxrs' Spring NamespaceHandler be available on classpath. Sometimes, due to classloading restrictions or bugs in underlying containers which are exposed during complex deployments or due to multiple Spring libraries interfering with each other, NamespaceHandler can not be located and thus jaxrs endpoints can not be created. <br/>
Please report such issues to the team working on developing the container itself.</p>

<p>If you need to do Spring configuration and get an error to do with a missing NamespaceHandler then, as a workaround, consider configuring jaxrs endpoints using CXF beans which actually handle the creation of jaxrs:server endpoints. This is marginally more complex, but overall, the configuration ends up being quite similar, for example, the above jaxrs:server endpoint can be configured like this instead:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span> encoding=<span class="code-quote">"UTF-8"</span>?&gt;</span>
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
  <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
  xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

  <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf.xml"</span> /&gt;</span>
  <span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-servlet.xml"</span> /&gt;</span>

  <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.cxf.jaxrs.JAXRSServerFactoryBean"</span> init-method=<span class="code-quote">"create"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"address"</span> value=<span class="code-quote">"/service1"</span>/&gt;</span>
    <span class="code-tag">&lt;property:serviceBeans&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"customerBean"</span> /&gt;</span>
    <span class="code-tag">&lt;/property:serviceBeans&gt;</span>
  <span class="code-tag">&lt;/jaxrs:server&gt;</span>

  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerService"</span> /&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>

</pre>
</div></div>


<h1><a name="JAXRSServicesConfiguration-ConfiguringJAXRSservicesincontainerwithoutSpring"></a>Configuring JAX-RS services in container without Spring</h1>

<p>If you prefer, you can register JAX-RS endpoints without depending on Spring with the help of CXFNonSpringJaxrsServlet :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;servlet&gt;</span>
 <span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet<span class="code-tag">&lt;/servlet-name&gt;</span>
 <span class="code-tag">&lt;display-name&gt;</span>CXF Servlet<span class="code-tag">&lt;/display-name&gt;</span>
 <span class="code-tag">&lt;servlet-class&gt;</span>
   org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
 <span class="code-tag">&lt;/servlet-class&gt;</span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.serviceClasses<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    org.apache.cxf.systest.jaxrs.BookStore1
    org.apache.cxf.systest.jaxrs.BookStore2		      
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.providers<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    org.apache.cxf.systest.jaxrs.BookStoreProvider1
    org.apache.cxf.systest.jaxrs.BookStoreProvider2		      
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
 <span class="code-tag"><span class="code-comment">&lt;!-- enables schema validation --&gt;</span></span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.schemaLocations<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    classpath:/WEB-INF/schemas/schema1.xsd
    classpath:/WEB-INF/schemas/schema2.xsd		      
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span> 
 <span class="code-tag"><span class="code-comment">&lt;!-- registers CXF in interceptors --&gt;</span></span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.inInterceptors<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    org.apache.cxf.systest.jaxrs.CustomInInterceptor
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span> 
 <span class="code-tag"><span class="code-comment">&lt;!-- registers CXF out interceptors --&gt;</span></span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.outInterceptors<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    org.apache.cxf.systest.jaxrs.CustomOutInterceptor
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
<span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
<span class="code-tag">&lt;/servlet&gt;</span>
</pre>
</div></div>

<p>When service classes and providers are registered this way, the default life-cycle is 'singleton'. You can override it by setting a "jaxrs.scope" parameter with the value of 'prototype' (equivalent to per-request). <br/>
By default, the endpoint address is "/". One can provide a more specific value using a "jaxrs.address" parameter.</p>

<p>If the referenced service classes are not annotated with JAX-RS annotations then an external user model can also be linked to :</p>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;servlet&gt;</span>
 <span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet<span class="code-tag">&lt;/servlet-name&gt;</span>
 <span class="code-tag">&lt;display-name&gt;</span>CXF Servlet<span class="code-tag">&lt;/display-name&gt;</span>
 <span class="code-tag">&lt;servlet-class&gt;</span>
   org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
 <span class="code-tag">&lt;/servlet-class&gt;</span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.serviceClasses<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    org.apache.cxf.systest.jaxrs.BookStore
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
 <span class="code-tag"><span class="code-comment">&lt;!-- link to the user model --&gt;</span></span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>user.model<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    classpath:/models/resources.xml
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span> 
 <span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
<span class="code-tag">&lt;/servlet&gt;</span>
</pre>
</div></div>

<p>A more portable way to register resource classes and providers with CXFNonSpringJaxrsServlet is to use a JAX-RS Application <a href="http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java" class="external-link" rel="nofollow">implementation</a> :</p>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;servlet&gt;</span>
 <span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet<span class="code-tag">&lt;/servlet-name&gt;</span>
 <span class="code-tag">&lt;display-name&gt;</span>CXF Servlet<span class="code-tag">&lt;/display-name&gt;</span>
 <span class="code-tag">&lt;servlet-class&gt;</span>
   org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
 <span class="code-tag">&lt;/servlet-class&gt;</span>
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>javax.ws.rs.Application<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>
    org.apache.cxf.systest.jaxrs.BookApplication	      
  <span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
 &lt;!-- 
    This parameter is recognized only starting from CXF 2.3.1
    @ApplicationPath value will be ignored if this parameter is set to true
 --&gt;
 <span class="code-tag">&lt;init-param&gt;</span>
  <span class="code-tag">&lt;param-name&gt;</span>jaxrs.application.address.ignore<span class="code-tag">&lt;/param-name&gt;</span>
  <span class="code-tag">&lt;param-value&gt;</span>true<span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
<span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
<span class="code-tag">&lt;/servlet&gt;</span>
</pre>
</div></div>

<p>Note that Application.getClasses() method returns a set of per-request resource class names. Application.getSingletons() returns a list of singleton resource and provider classes. </p>


<h2><a name="JAXRSServicesConfiguration-AttachingJAXRSendpointstoanexistingJettyserver"></a>Attaching JAXRS endpoints to an existing Jetty server</h2>

<p>Here is a code fragment showing how it can be done with the help of CxfNonSpringJaxrsServlet :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CXFNonSpringJAXRSServlet cxf = <span class="code-keyword">new</span> CXFNonSpringJaxrsServlet();

...

ServletHolder servlet = <span class="code-keyword">new</span> ServletHolder(cxf);
servlet.setInitParameter(<span class="code-quote">"javax.ws.rs.Application"</span>, <span class="code-quote">"com.acme.MyServiceImpl"</span>);
servlet.setName(<span class="code-quote">"services"</span>);
servlet.setForcedPath(<span class="code-quote">"services"</span>);
root.addServlet(servlet, <span class="code-quote">"/*"</span>);

</pre>
</div></div>

<h1><a name="JAXRSServicesConfiguration-JAXRSRuntimeDelegateandApplications"></a>JAX-RS RuntimeDelegate and Applications</h1>

<p>If you have a JAX-RS Application implementation available and would like to minimize the interaction with the CXF JAX-RS specific API, you may want to use the JAX-RS RuntimeDelegate :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

<span class="code-keyword">import</span> javax.ws.rs.ext.RuntimeDelegate;
<span class="code-keyword">import</span> org.apache.cxf.endpoint.Server;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

RuntimeDelegate delegate = RuntimeDelegate.getInstance();
JAXRSServerFactoryBean bean = delegate.createEndpoint(<span class="code-keyword">new</span> CustomApplication(), JAXRSServerFactoryBean.class);

<span class="code-comment">// before CXF 2.3.1 :
</span><span class="code-comment">// bean.setAddress(<span class="code-quote">"http://localhost:8080/services"</span>);
</span>
bean.setAddress(<span class="code-quote">"http:<span class="code-comment">//localhost:8080/services"</span> + bean.getAddress());
</span>
Server server = bean.create();
server.start();
<span class="code-comment">// and <span class="code-keyword">finally</span>
</span>server.stop();

</pre>
</div></div>

<p>Note that the above code makes sure an @ApplicationPath value (if CustomApplication has this annotation) is taken into account.</p>

<h1><a name="JAXRSServicesConfiguration-ConfiguringJAXRSservicesprogrammaticallywithSpringconfigurationfile."></a>Configuring JAX-RS services programmatically with Spring configuration file. </h1>

<p>When using Spring explicitly in your code, you may want to follow this example :</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ClassPathXmlApplicationContext ctx = <span class="code-keyword">new</span> ClassPathXmlApplicationContext(<span class="code-keyword">new</span> <span class="code-object">String</span>[]
                      {<span class="code-quote">"/org/apache/cxf/jaxrs/spring/servers.xml"</span>});

<span class="code-comment">// 'simple' is the id of the jaxrs server bean
</span>JAXRSServerFactoryBean sfb = (JAXRSServerFactoryBean)ctx.getBean(<span class="code-quote">"simple"</span>);
sfb.create();
</pre>
</div></div>

<p>Note that in in this case your Spring configuration file should import cxf-extension-http-jetty.xml instead of cxf-servlet.xml :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;!--
<span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-servlet.xml"</span> /&gt;</span>
--&gt;
<span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-extension-http-jetty.xml"</span> /&gt;</span>
</pre>
</div></div>

<h1><a name="JAXRSServicesConfiguration-Lifecyclemanagement"></a>Lifecycle management</h1>

<h2><a name="JAXRSServicesConfiguration-FromSpring"></a>From Spring</h2>

<p>By default, the service beans which are referenced directly from the jaxrs:server endpoint declarations are treated by the runtime as singleton JAX-RS root resources. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans&gt;</span>
  <span class="code-tag">&lt;jaxrs:server id=<span class="code-quote">"customerService"</span> address=<span class="code-quote">"/service1"</span>&gt;</span>
    <span class="code-tag">&lt;jaxrs:serviceBeans&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"customerBean"</span> /&gt;</span>
    <span class="code-tag">&lt;/jaxrs:serviceBeans&gt;</span>
  <span class="code-tag">&lt;/jaxrs:server&gt;</span>
  &lt;!--
    The scope attribute, if any, will be ignored. For example, adding the scope attribute with the value
    <span class="code-quote">"request"</span>, <span class="code-quote">"prototype"</span>, etc, won't have any effect, the customerBean will still be treated as 
    a JAX-RS singleton by the runtime.
  --&gt; 
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerService"</span> /&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>Spring instantiates and injects the customerBean reference and the runtime will access this reference directly afterwards. Effectively, the scope attribute which may be present on the customerBean bean declaration is ignored in this case, <b>unless the Spring AOP is used to enforce the required scope</b> (see below for more information). </p>

<p>The 'serviceFactories' element or beanNames attribute has to be used for a 'prototype', 'request' and other Spring bean scopes be supported.</p>

<p>For example, the serviceFactories element can reference one or more beans of type 'org.apache.cxf.jaxrs.spring.SpringResourceFactory' which in turn reference the actual service beans.   </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans&gt;</span>
  <span class="code-tag">&lt;jaxrs:server id=<span class="code-quote">"customerService"</span> address=<span class="code-quote">"/service1"</span>&gt;</span>
    <span class="code-tag">&lt;jaxrs:serviceFactories&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"sfactory1"</span> /&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"sfactory2"</span> /&gt;</span> 
    <span class="code-tag">&lt;/jaxrs:serviceFactories&gt;</span>
  <span class="code-tag">&lt;/jaxrs:server&gt;</span>
  
  <span class="code-tag">&lt;bean id=<span class="code-quote">"sfactory1"</span> class=<span class="code-quote">"org.apache.cxf.jaxrs.spring.SpringResourceFactory"</span>&gt;</span>
     <span class="code-tag">&lt;property name=<span class="code-quote">"beanId"</span> value=<span class="code-quote">"customerBean1"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"sfactory2"</span> class=<span class="code-quote">"org.apache.cxf.jaxrs.spring.SpringResourceFactory"</span>&gt;</span>
     <span class="code-tag">&lt;property name=<span class="code-quote">"beanId"</span> value=<span class="code-quote">"customerBean2"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>

  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean1"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerRootResource1"</span> scope=<span class="code-quote">"prototype"</span>/&gt;</span> 
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean2"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerRootResource2"</span>  scope=<span class="code-quote">"prototype"</span>/&gt;</span> 
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>In this example, the jaxrs:server endpoint has two JAX-RS root resources (customerBean1 and customerBean2) with the Spring 'prototype' scope.<br/>
Other scopes can also be supported.</p>

<p>If using the jaxrs:serviceFactories element seems a bit verbose then the 'beanNames' attribute can be used instead:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans&gt;</span>
  &lt;jaxrs:server id=<span class="code-quote">"customerService"</span> address=<span class="code-quote">"/service1"</span>
    beanNames=<span class="code-quote">"customerBean1 customerBean2"</span>/&gt;
  
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean1"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerRootResource1"</span> scope=<span class="code-quote">"prototype"</span>/&gt;</span> 
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean2"</span> class=<span class="code-quote">"demo.jaxrs.server.CustomerRootResource2"</span>  scope=<span class="code-quote">"prototype"</span>/&gt;</span> 
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>The beanNames attribute lists the names/ids of service beans separated by space. The jaxrs:serviceFactories element has to be used when users register custom CXF JAX-RS <a href="http://svn.apache.org/repos/asf/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/lifecycle/ResourceProvider.java" class="external-link" rel="nofollow">ResourceProvider</a> implementations.</p>

<p>Another approach toward supporting complex scopes in Spring is to use Spring AOP. For example, the following fragment shows how to have the Spring "request" scope supported:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;beans
	xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
	<span class="code-keyword">xmlns:aop</span>=<span class="code-quote">"http://www.springframework.org/schema/aop"</span>
	<span class="code-keyword">xmlns:jaxrs</span>=<span class="code-quote">"http://cxf.apache.org/jaxrs"</span>
	<span class="code-keyword">xmlns:cxf</span>=<span class="code-quote">"http://cxf.apache.org/core"</span>
	<span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://cxf.apache.org/jaxrs                   http://cxf.apache.org/schemas/jaxrs.xsd
		http://cxf.apache.org/core                    http://cxf.apache.org/schemas/core.xsd"&gt;

	<span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf.xml"</span>/&gt;</span>
	<span class="code-tag">&lt;import resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-servlet.xml"</span>/&gt;</span>

	<span class="code-tag">&lt;bean class=<span class="code-quote">"org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"</span>/&gt;</span>

	<span class="code-tag">&lt;jaxrs:server id=<span class="code-quote">"example"</span> address=<span class="code-quote">"/"</span>&gt;</span>
       	    <span class="code-tag">&lt;jaxrs:serviceBeans&gt;</span>
   	        <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.cxf.systest.jaxrs.CustomerService"</span> scope=<span class="code-quote">"request"</span>&gt;</span><span class="code-tag">&lt;aop:scoped-proxy /&gt;</span><span class="code-tag">&lt;/bean&gt;</span>
	    <span class="code-tag">&lt;/jaxrs:serviceBeans&gt;</span>
	<span class="code-tag">&lt;/jaxrs:server&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>in addition, the following servlet listener has to be added to the web.xml:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;listener&gt;</span>
    <span class="code-tag">&lt;listener-class&gt;</span>org.springframework.web.context.request.RequestContextListener<span class="code-tag">&lt;/listener-class&gt;</span>
<span class="code-tag">&lt;/listener&gt;</span>
</pre>
</div></div>

<p>The request-scoped service bean instances (example, org.apache.cxf.systest.jaxrs.CustomerService instances) are not actually available at the initialization time thus one limitation of the above configuration is that it is not possible to inject JAX-RS contexts into these service beans. This is not a show-stopper because contexts such as UriInfo can be passed in as resource method parameters. However, if the injection into the fields or via method setters is required then a little customization of the org.apache.cxf.jaxrs.spring.SpringResourceFactory will do the trick. Particularly, the Spring ApplicationContext reports that a request-scoped bean is a singleton but the JAX-RS runtime can not inject thread-local proxies given that the actual instance is not available as explained above; in fact, the request-scoped beans are not really JAX-RS singletons. Thus a simple custom factory like <a href="http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java" class="external-link" rel="nofollow">this one</a> is needed and it has to be used the following way:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans&gt;</span>
  <span class="code-tag">&lt;jaxrs:server id=<span class="code-quote">"customerService"</span> address=<span class="code-quote">"/service1"</span>&gt;</span>
    <span class="code-tag">&lt;jaxrs:serviceFactories&gt;</span>
       <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.cxf.systest.jaxrs.RequestScopeResourceFactory"</span>&gt;</span>
          <span class="code-tag">&lt;property name=<span class="code-quote">"beanId"</span> value=<span class="code-quote">"customerBean"</span>/&gt;</span>
       <span class="code-tag">&lt;/bean&gt;</span> 
    <span class="code-tag">&lt;/jaxrs:serviceFactories&gt;</span>
  <span class="code-tag">&lt;/jaxrs:server&gt;</span>
  
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean"</span> class=<span class="code-quote">"org.apache.cxf.systest.jaxrs.CustomerService"</span> scope=<span class="code-quote">"request"</span>&gt;</span><span class="code-tag">&lt;aop:scoped-proxy /&gt;</span><span class="code-tag">&lt;/bean&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>The above configuration makes sure that the CXF JAX-RS runtime injects the values at the request time given that the customerBean bean is not seen as a JAX-RS singleton. This approach is only needed if the injection of contexts is required.</p>

<h2><a name="JAXRSServicesConfiguration-WithCXFNonSpringJaxrsServlet"></a>With CXFNonSpringJaxrsServlet</h2>

<p>CXFNonSpringJaxrsServlet uses 'Singleton' as a default scope for service classes specified by a "jaxrs.serviceClasses" servlet parameter. It can be overridden by setting a "jaxrs.scope" parameter to a "prototype" value or by not using the "jaxrs.serviceClasses" parameter at all and registering a JAXRS Application implementation instead. Please see the section describing CXFNonSpringJaxrsServlet for more details.</p>

<p>CXFNonSpringJaxrsServlet can support singleton scopes for classes with constructors expecting JAXRS contexts, at the moment it can only inject ServletContext or ServletConfig contexts :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@Path(<span class="code-quote">"/"</span>)
<span class="code-keyword">public</span> class SingletonResourceClass {
   <span class="code-keyword">public</span> SingletonResourceClass(@Context ServletContext context, @Context ServletConfig context2) {}
}
</pre>
</div></div> 

<h2><a name="JAXRSServicesConfiguration-Programmatically"></a>Programmatically</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JAXRSServerFactoryBean sf = <span class="code-keyword">new</span> JAXRSServerFactoryBean();
sf.setResourceClass(CustomerService.class);
sf.setResourceProvider(<span class="code-keyword">new</span> SingletonResourceProvider(<span class="code-keyword">new</span> CustomerService()));
sf.setResourceClass(CustomerService2.class);
sf.setResourceProvider(<span class="code-keyword">new</span> PerRequestResourceProvider(CustomerService.class));
</pre>
</div></div>

<h2><a name="JAXRSServicesConfiguration-PostConstructandPreDestroy"></a>PostConstruct and PreDestroy</h2>

<p>Bean methods annotated with @PostConstruct and @PreDestroy annotations will be called as expected by the scope rules. <br/>
Singleton beans will have their postconstruct method called when the endpoint is created. If a given singleton resource instance was created by Spring then its predestroy method will also be called after, for example, the web application which uses it is about to be unloaded. At the moment singletons created by CXFNonSpringJaxrsServlet or programmatically will only have their postconstruct method (if any) called.  </p>

<p>Prototype beans will have their postconstruct and predestroy method called before a resource method is invoked and immediately after the invocation has returned but before the response has actually been serialized. You can indicate that the predestroy method has to be called after the request has completely gone out of scope (that is after the response body if any has been written to the output stream) by adding an "org.apache.cxf.jaxrs.service.scope" property with the value set to "request".</p>

<p>You can also register a custom Spring resource factory by extending org.apache.cxf.jaxrs.spring.SpringResourceFactory or providing a more sophisticated implementation.</p>

<h1><a name="JAXRSServicesConfiguration-Locatingcustomresourcesinwebapplications"></a>Locating custom resources in web applications</h1>

<p>Resources like schemas, custom XSLT templates and user models are typically referenced using a classpath: prefix. Thus one can add them to a WEB-INF/classes folder in a given web application.<br/>
Since CXF 2.2.3 one can put them directly under WEB-INF, for example into WEB-INF/xslt,  WEB-INF/schemas, WEB-INF/model and referencing them like 'classpath:/WEB-INF/xslt/template.xsl'.</p>

<h1><a name="JAXRSServicesConfiguration-Multipleendpointsandresourceclasses"></a>Multiple endpoints and resource classes</h1>

<p>One can configure as many jaxrs:server endpoints as needed for a given application, with every endpoint possibly providing an alternative path to a single resource bean. Every endpoint can employ as many shared or unique resource classes as needed, and have common or different providers.  </p>

<h1><a name="JAXRSServicesConfiguration-DynamicservletsandasingleJAXRSendpoint"></a>Dynamic servlets and a single JAX-RS endpoint</h1>

<p>In some advanced cases you may want to dynamically add new servlets (CXFServlet or CXFNonSpringJaxrsServlet) with all of them serving the same JAX-RS endpoints. In this case you most likely want to configure servlets so that the CXF endpoint address is not overridden :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
{code:xml}
<span class="code-tag">&lt;servlet&gt;</span>
 <span class="code-tag">&lt;servlet-name&gt;</span>CXFServlet<span class="code-tag">&lt;/servlet-name&gt;</span>
 <span class="code-tag">&lt;display-name&gt;</span>CXF Servlet<span class="code-tag">&lt;/display-name&gt;</span>
 <span class="code-tag">&lt;servlet-class&gt;</span>
     org.apache.cxf.transport.servlet.CXFServlet
 <span class="code-tag">&lt;/servlet-class&gt;</span>
 <span class="code-tag">&lt;init-param&gt;</span>
    <span class="code-tag">&lt;param-name&gt;</span>config-location<span class="code-tag">&lt;/param-name&gt;</span>
    <span class="code-tag">&lt;param-value&gt;</span>/WEB-INF/beans1.xml<span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
 <span class="code-tag">&lt;init-param&gt;</span>
    <span class="code-tag">&lt;param-name&gt;</span>disable-address-updates<span class="code-tag">&lt;/param-name&gt;</span>
    <span class="code-tag">&lt;param-value&gt;</span>true<span class="code-tag">&lt;/param-value&gt;</span>
 <span class="code-tag">&lt;/init-param&gt;</span>
 <span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
<span class="code-tag">&lt;/servlet&gt;</span>
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"></pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Services+Configuration">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=24190821&revisedVersion=12&originalVersion=11">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Services+Configuration?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message