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 > Writing a service with Spring
Date Tue, 29 Nov 2011 18:32: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/Writing+a+service+with+Spring">Writing
a service with Spring</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~mazzag">Glen
Mazza</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        updated page per updates made to underlying java-first sample.<br />
    </div>
        <br/>
                         <h4>Changes (9)</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" >* Set up the HTTP transport <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >This example corresponds to the
<span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">spring_http</span>
<span class="diff-added-words"style="background-color: #dfd;">java_first_spring_support</span>
example in the CXF distribution. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h1. Setting up your build
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h1. Writing your Service <br>First
we&#39;ll write our service interface. It will have one operation called &quot;sayHello&quot;
which says &quot;Hello&quot; to whoever submits their name. <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/<span
class="diff-added-chars"style="background-color: #dfd;">main/java/</span>demo/spring/<span
class="diff-added-chars"style="background-color: #dfd;">service/</span>HelloWorld.java?revision=HEAD}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Our implementation will
then look like this: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/<span
class="diff-added-chars"style="background-color: #dfd;">main/java/</span>demo/spring/<span
class="diff-added-chars"style="background-color: #dfd;">service</span>/HelloWorldImpl.java?revision=HEAD}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>The {{@WebService}} annotation
on the implementation class lets CXF know which interface to use when creating WSDL. In this
case its simply our HelloWorld interface. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >CXF contains support for &quot;nice
XML&quot; within Spring 2.0. For the JAX-WS side of things, we have a &lt;jaxws:endpoint&gt;
bean which sets up a server side endpoint. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Lets create a <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&quot;beans.xml&quot;</span>
<span class="diff-added-words"style="background-color: #dfd;">&quot;cxf-servlet.xml&quot;</span>
file in our WEB-INF directory which declares an endpoint bean: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">{snippet:id=beans|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/webapp/WEB-INF/beans.xml?revision=HEAD}</span>
<span class="diff-added-words"style="background-color: #dfd;">{snippet:id=beans|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/webapp/WEB-INF/cxf-servlet.xml?revision=HEAD}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>If you want to reference
a spring managed-bean, you can write like this: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h1. Setting up the Servlet
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">We&#39;ll
need to add two things to our web.xml file: <br># the Spring {{ContextLoaderLister}}.
This starts Spring and loads our beans.xml file. We can specify where our file is via a {{context-param}}
element. <br># the CXF Servlet <br>{snippet:id=webxml|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/webapp/WEB-INF/web.xml?revision=HEAD}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Since
we&#39;re relying on the default &quot;cxf-servlet.xml&quot; file the [default
web.xml|http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/etc/web.xml?revision=HEAD]
referenced by many samples can be used. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Alternatively,
for arbitrarily named configuration files such as beans.xml, application-context.xml, etc.
we can add the following elements: <br># the Spring {{ContextLoaderLister}}. This starts
Spring and explicitly loads the configuration file. We can specify where our file is via a
{{context-param}} element. <br> <br>An example: <br>{code:java} <br>&lt;web-app
...&gt; <br>... <br>   &lt;context-param&gt; <br>      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
<br>      &lt;param-value&gt;WEB-INF/beans.xml&lt;/param-value&gt; <br>
  &lt;/context-param&gt; <br> <br>   &lt;listener&gt; <br>
     &lt;listener-class&gt; <br>         org.springframework.web.context.ContextLoaderListener
<br>      &lt;/listener-class&gt; <br>   &lt;/listener&gt; <br>&lt;/web-app&gt;
<br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >It is important to note that the address
that you chose for your endpoint bean must be one your servlet listens on. For instance, if
my Servlet was register for &quot;/some-services/*&quot; but my address was &quot;/more-services/HelloWorld&quot;,
there is no way CXF could receive a request.  <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code:java} <br>ApplicationContext
context = ...; // your Spring ApplicationContext <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">Hell<span
class="diff-added-chars"style="background-color: #dfd;">o</span>World</span>
client = (HelloWorld) context.getBean(&quot;client&quot;); <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>This example will lead you through creating your first service with <a
href="http://springframework.org" class="external-link" rel="nofollow">Spring</a>.
You'll learn how to:</p>
<ul>
	<li>Set up your build for CXF</li>
	<li>Writing a simple JAX-WS service</li>
	<li>Set up the HTTP transport</li>
</ul>


<p>This example corresponds to the java_first_spring_support example in the CXF distribution.
</p>

<h1><a name="WritingaservicewithSpring-Settingupyourbuild"></a>Setting up
your build</h1>
<p>Open up your favorite IDE and create a new project. The first thing we need to do
is add the necessary CXF dependencies to the project. You can find these dependencies in the
CXF distribution in the lib directory.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.7.1.jar (or Sun's JavaMail jar)
geronimo-servlet_3.0_spec-1.0.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.13.jar
jaxws-api-2.1.jar
neethi-3.0.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
stax2-api-3.1.1.jar
wsdl4j-1.6.2.jar
woodstox-core-asl-4.0.8.jar
xmlschema-core-2.0.jar
xml-resolver-1.2.jar
</pre>
</div></div>

<p>The Spring jars:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
aopalliance-1.0.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
</pre>
</div></div>

<p>And the CXF jar:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cxf-2.4.0.jar
</pre>
</div></div>

<h1><a name="WritingaservicewithSpring-WritingyourService"></a>Writing your
Service</h1>
<p>First we'll write our service interface. It will have one operation called "sayHello"
which says "Hello" to whoever submits their name.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">package</span> demo.spring.service;

<span class="code-keyword">import</span> javax.jws.WebService;

@WebService
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
HelloWorld {
    <span class="code-object">String</span> sayHi(<span class="code-object">String</span>
text);
}
</pre>
</div></div>

<p>Our implementation will then look like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">package</span> demo.spring.service;

<span class="code-keyword">import</span> javax.jws.WebService;

@WebService(endpointInterface = <span class="code-quote">"demo.spring.service.HelloWorld"</span>)
<span class="code-keyword">public</span> class HelloWorldImpl <span class="code-keyword">implements</span>
HelloWorld {

    <span class="code-keyword">public</span> <span class="code-object">String</span>
sayHi(<span class="code-object">String</span> text) {
        <span class="code-object">System</span>.out.println(<span class="code-quote">"sayHi
called"</span>);
        <span class="code-keyword">return</span> <span class="code-quote">"Hello
"</span> + text;
    }
}
</pre>
</div></div>

<p>The <tt>@WebService</tt> annotation on the implementation class lets
CXF know which interface to use when creating WSDL. In this case its simply our HelloWorld
interface.</p>

<h1><a name="WritingaservicewithSpring-Declaringyourserverbeans"></a>Declaring
your server beans</h1>
<p>CXF contains support for "nice XML" within Spring 2.0. For the JAX-WS side of things,
we have a &lt;jaxws:endpoint&gt; bean which sets up a server side endpoint.</p>

<p>Lets create a "cxf-servlet.xml" file in our WEB-INF directory which declares an endpoint
bean:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">&lt;beans xmlns=<span class="code-quote">"http:<span
class="code-comment">//www.springframework.org/schema/beans"</span>
</span>	xmlns:xsi=<span class="code-quote">"http:<span class="code-comment">//www.w3.org/2001/XMLSchema-instance"</span>
</span>	xmlns:jaxws=<span class="code-quote">"http:<span class="code-comment">//cxf.apache.org/jaxws"</span>
</span>	xsi:schemaLocation="
http:<span class="code-comment">//www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
</span>http:<span class="code-comment">//cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"&gt;
</span>
	&lt;<span class="code-keyword">import</span> resource=<span class="code-quote">"classpath:META-INF/cxf/cxf.xml"</span>
/&gt;
	&lt;<span class="code-keyword">import</span> resource=<span class="code-quote">"classpath:META-INF/cxf/cxf-servlet.xml"</span>
/&gt;

	&lt;jaxws:endpoint 
	  id=<span class="code-quote">"helloWorld"</span> 
	  implementor=<span class="code-quote">"demo.spring.service.HelloWorldImpl"</span>

	  address=<span class="code-quote">"/HelloWorld"</span> /&gt;
	  
&lt;/beans&gt;
</pre>
</div></div>

<p>If you want to reference a spring managed-bean, you can write like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"hello"</span>
class=<span class="code-quote">"demo.spring.HelloWorldImpl"</span> /&gt;</span>

<span class="code-tag">&lt;jaxws:endpoint id=<span class="code-quote">"helloWorld"</span>
implementor=<span class="code-quote">"#hello"</span> address=<span class="code-quote">"/HelloWorld"</span>
/&gt;</span>
</pre>
</div></div>
<p>The bean uses the following properties:</p>
<ul>
	<li><tt>id</tt> specifies the id of the bean in the Spring context.</li>
	<li><tt>implementor</tt> specifies the implementation class.</li>
	<li><tt>address</tt> specifies the location the service will be hosted.
This should just be a related path. This is because CXF can't know the war name and the servlet
container's listening port, CXF will update the endpoint address with the request url at the
runtime.</li>
</ul>


<p>To provide a bean name instead of a classname as an implementor, simply supply the
bean-name prepended with "#", e.g. implementor="#myBean".</p>

<p>You can also do more sophisticated things with the <tt>&lt;jaxws:endpoint&gt;</tt>
element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service.
 For more on this see <a href="/confluence/display/CXF20DOC/JAX-WS+Configuration" title="JAX-WS
Configuration">JAX&#45;WS Configuration</a>.</p>

<h1><a name="WritingaservicewithSpring-SettinguptheServlet"></a>Setting
up the Servlet</h1>
<p>Since we're relying on the default "cxf-servlet.xml" file the <a href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/etc/web.xml?revision=HEAD"
class="external-link" rel="nofollow">default web.xml</a> referenced by many samples
can be used.</p>

<p>Alternatively, for arbitrarily named configuration files such as beans.xml, application-context.xml,
etc. we can add the following elements:</p>
<ol>
	<li>the Spring <tt>ContextLoaderLister</tt>. This starts Spring and explicitly
loads the configuration file. We can specify where our file is via a <tt>context-param</tt>
element.</li>
</ol>


<p>An example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;web-app ...&gt;
...
   &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;WEB-INF/beans.xml&lt;/param-value&gt;
   &lt;/context-param&gt;

   &lt;listener&gt;
      &lt;listener-class&gt;
         org.springframework.web.context.ContextLoaderListener
      &lt;/listener-class&gt;
   &lt;/listener&gt;
&lt;/web-app&gt;
</pre>
</div></div>

<p>It is important to note that the address that you chose for your endpoint bean must
be one your servlet listens on. For instance, if my Servlet was register for "/some-services/*"
but my address was "/more-services/HelloWorld", there is no way CXF could receive a request.
</p>

<h1><a name="WritingaservicewithSpring-CreateaClient%28EasyWay%29"></a>Create
a Client (Easy Way)</h1>
<p>Just like the <tt>&lt;jaxws:endpoint&gt;</tt> used on the server
side, there is a <tt>&lt;jaxws:client&gt;</tt> that can be used on the
client side.  You'll give it a bean name, the service interface, and the service URL, and
it will create a bean with the specified name, implementing the service interface, and invoking
the remote SOAP service under the covers:</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:jaxws</span>=<span class="code-quote">"http://cxf.apache.org/jaxws"</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
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"&gt;

    &lt;jaxws:client id=<span class="code-quote">"helloClient"</span>
                  serviceClass=<span class="code-quote">"demo.spring.HelloWorld"</span>
                  address=<span class="code-quote">"http://localhost:9002/HelloWorld"</span>
/&gt;
<span class="code-tag">&lt;/beans&gt;</span></pre>
</div></div>

<p>You can now inject that "helloClient" bean into any other Spring bean, or look it
up from the Spring application context manually with code like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ApplicationContext context = ...; <span class="code-comment">// your Spring ApplicationContext
</span>HellWorld client = (HelloWorld) context.getBean(<span class="code-quote">"helloClient"</span>);
</pre>
</div></div>

<p>You can also do more sophisticated things with the <tt>&lt;jaxws:client&gt;</tt>
element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client.
 For more on this see <a href="/confluence/display/CXF20DOC/JAX-WS+Configuration" title="JAX-WS
Configuration">JAX&#45;WS Configuration</a>.</p>

<h1><a name="WritingaservicewithSpring-CreateaClient%28MoreManualWay%29"></a>Create
a Client (More Manual Way)</h1>
<p>CXF includes a JaxWsProxyFactory bean which create a client for you from your service
interface. You simply need to tell it what your service class is (the HelloWorld interface
in this case) and the URL of your service. You can then create a client bean via the JaxWsProxyFactory
bean by calling it's create() method.</p>

<p>Here's an example:</p>
<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<p>If you were going to access your client you could now simply pull it out of the Spring
context (or better yet, inject it into your application using Spring!):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ApplicationContext context = ...; <span class="code-comment">// your Spring ApplicationContext
</span>HelloWorld client = (HelloWorld) context.getBean(<span class="code-quote">"client"</span>);
</pre>
</div></div>

<p>client code at 
<a href="http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/demo/spring/client/Client.java"
class="external-link" rel="nofollow">http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/demo/spring/client/Client.java</a></p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>Some usage scenarios will require
more extensive configuration (and this is not the case with the <tt>&lt;jaxws:client&gt;</tt>
syntax described above).  For more information, see <a href="/confluence/display/CXF20DOC/JAX-WS+Configuration"
title="JAX-WS Configuration">JAX&#45;WS Configuration</a>.</td></tr></table></div>

<h1><a name="WritingaservicewithSpring-AdvancedSteps"></a>Advanced Steps</h1>
<p>For more information on using Spring you may want to read the <a href="/confluence/display/CXF20DOC/Configuration"
title="Configuration">Configuration</a> and <a href="/confluence/display/CXF20DOC/Spring"
title="Spring">Spring</a> sections of the User's Guide.</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/Writing+a+service+with+Spring">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=48729&revisedVersion=26&originalVersion=25">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/Writing+a+service+with+Spring?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message