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 Tue, 21 Dec 2010 23:04:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~sergey_beryozkin">Sergey
Beryozkin</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <span style="font-size:2em;font-weight:bold"> JAX-RS : Configuration </span>

<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-ConfiguringJAXRSclientsprogrammaticallywithoutSpring'>Configuring
JAX-RS clients 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-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-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>
</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">
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>A couple of things to note:</p>
<ul>
	<li>The JAXRSServerFactoryBean creates a Server inside CXF which starts listening for
requests on the URL specified.</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-ConfiguringJAXRSclientsprogrammaticallywithoutSpring"></a>Configuring
JAX-RS clients programmatically without Spring</h1>

<p>Example :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JAXRSClientFactoryBean sf = <span class="code-keyword">new</span> JAXRSClientFactoryBean();
sf.setResourceClass(CustomerService.class);
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);
CustomerService service = sf.create(CustomerService.class);
</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-extension-jaxrs-binding.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-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>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>
<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-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>The singleton scope is applied to all service beans which are injected like this
:</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>
  <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>You can support prototypes by either using a beanNames attribute or schemaFactories
element :</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">"customerBean2 customerBean3"</span>&gt;
    <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: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">"customerBean"</span>
class=<span class="code-quote">"demo.jaxrs.server.CustomerService"</span> /&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean2"</span>
class=<span class="code-quote">"demo.jaxrs.server.CustomerService2"</span>  scope=<span
class="code-quote">"prototype"</span>/&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean3"</span>
class=<span class="code-quote">"demo.jaxrs.server.CustomerService3"</span>  scope=<span
class="code-quote">"prototype"</span>/&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">"beanName"</span>
value=<span class="code-quote">"customerBean4"</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">"beanName"</span>
value=<span class="code-quote">"customerBean5"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>

  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean4"</span>
class=<span class="code-quote">"demo.jaxrs.server.CustomerService4"</span> scope=<span
class="code-quote">"prototype"</span>/&gt;</span> 
  <span class="code-tag">&lt;bean id=<span class="code-quote">"customerBean5"</span>
class=<span class="code-quote">"demo.jaxrs.server.CustomerService5"</span>  scope=<span
class="code-quote">"prototype"</span>/&gt;</span> 
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>


<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>

    </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/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