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 > A simple JAX-WS service
Date Mon, 24 Aug 2009 14:46:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CXF20DOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service">A
simple JAX-WS service</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~mazzag">Glen
Mazza</a>
    </h4>
     Switched to regular Maven tools page for Maven reference.
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Switched to regular Maven tools page for Maven reference.<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <p>This example will lead you through creating your first service with doing
"code first" development with JAX-WS.</p>

<div>
<ul>
    <li><a href='#AsimpleJAX-WSservice-Settingupyourbuild'>Setting up your build</a></li>
    <li><a href='#AsimpleJAX-WSservice-WritingyourService'>Writing your Service</a></li>
    <li><a href='#AsimpleJAX-WSservice-Publishingyourservice'>Publishing your
service</a></li>
    <li><a href='#AsimpleJAX-WSservice-Accessingyourservice'>Accessing your service</a></li>
</ul></div>


<p>This example corresponds to the hello_world_code_first example in the CXF distribution.
<b>IMPORTANT</b>: This sample is only in CXF 2.0.1+!</p>

<h1><a name="AsimpleJAX-WSservice-Settingupyourbuild"></a>Setting up your
build</h1>

<p>If you're using Maven to build your project have a look at <a href="http://cwiki.apache.org/confluence/display/CXF20DOC/Maven+Integration+and+Plugin"
rel="nofollow">this page</a>.</p>

<p>Otherwise, 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.  (note: the version numbers on these jars may
be different if versions have changed)</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
commons-logging-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.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
jaxb-api-2.1.jar
jaxb-impl-2.1.9.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.3.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.1.jar
XmlSchema-1.4.3.jar
xml-resolver-1.2.jar
</pre>
</div></div>

<p>The Spring jars (optional - for XML Configuration support):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.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.1.jar
</pre>
</div></div>

<h1><a name="AsimpleJAX-WSservice-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">
@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);


    /* Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
     * support interfaces directly.  Special XmlAdapter classes need to
     * be written to handle them
     */
    <span class="code-object">String</span> sayHiToUser(User user);


    /* Map passing
     * JAXB also does not support Maps.  It handles Lists great, but Maps are
     * not supported directly.  They also require use of a XmlAdapter to map
     * the maps into beans that JAXB can use. 
     */
    @XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
    Map&lt;<span class="code-object">Integer</span>, User&gt; getUsers();
}
</pre>
</div></div>

<p>To make sure your parameter is named correctly in the xml you should use:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@WebService
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
HelloWorld {
    <span class="code-object">String</span> sayHi(@WebParam(name=<span class="code-quote">"text"</span>)
<span class="code-object">String</span> text);
}
</pre>
</div></div>

<p>The @WebParam annotation is necessary as java interfaces do not store the Parameter
name in the .class file. So if you leave out the annotation your parameter will be named arg0.</p>

<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.hw.server;

<span class="code-keyword">import</span> java.util.LinkedHashMap;
<span class="code-keyword">import</span> java.util.Map;

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

@WebService(endpointInterface = <span class="code-quote">"demo.hw.server.HelloWorld"</span>,
            serviceName = <span class="code-quote">"HelloWorld"</span>)
<span class="code-keyword">public</span> class HelloWorldImpl <span class="code-keyword">implements</span>
HelloWorld {
    Map&lt;<span class="code-object">Integer</span>, User&gt; users =
<span class="code-keyword">new</span> LinkedHashMap&lt;<span class="code-object">Integer</span>,
User&gt;();


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

    <span class="code-keyword">public</span> <span class="code-object">String</span>
sayHiToUser(User user) {
        <span class="code-object">System</span>.out.println(<span class="code-quote">"sayHiToUser
called"</span>);
        users.put(users.size() + 1, user);
        <span class="code-keyword">return</span> <span class="code-quote">"Hello
"</span>  + user.getName();
    }

    <span class="code-keyword">public</span> Map&lt;<span class="code-object">Integer</span>,
User&gt; getUsers() {
        <span class="code-object">System</span>.out.println(<span class="code-quote">"getUsers
called"</span>);
        <span class="code-keyword">return</span> users;
    }

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

<p>The @WebService annotation on the implementation class lets CXF know which interface
we want to create our WSDL with. In this case its simply our HelloWorld interface.</p>

<h1><a name="AsimpleJAX-WSservice-Publishingyourservice"></a>Publishing
your service</h1>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-object">System</span>.out.println(<span
class="code-quote">"Starting Server"</span>);
HelloWorldImpl implementor = <span class="code-keyword">new</span> HelloWorldImpl();
<span class="code-object">String</span> address = <span class="code-quote">"http:<span
class="code-comment">//localhost:9000/helloWorld"</span>;
</span>Endpoint.publish(address, implementor);
</pre>
</div></div>

<p>whole code at
<a href="http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/server/Server.java"
rel="nofollow">http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/server/Server.java</a></p>

<p>Alternatively you can use the follwing code. This gives you more control over the
behaviour. For example you can add a logging interceptor:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
HelloWorldImpl implementor = <span class="code-keyword">new</span> HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = <span class="code-keyword">new</span> JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress(<span class="code-quote">"http:<span class="code-comment">//localhost:9000/helloWorld"</span>);
</span>svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(<span class="code-keyword">new</span> LoggingInInterceptor());
svrFactory.getOutInterceptors().add(<span class="code-keyword">new</span> LoggingOutInterceptor());
svrFactory.create();
</pre>
</div></div>

<p>You could leave out the ServiceClass. But it is better to use it so the server and
the client are created from the same interface. If you instead only use the implementation
class subtle problems may occur.</p>

<p>Pointing your browser at <a href="http://localhost:9000/helloWorld?wsdl" rel="nofollow">http://localhost:9000/helloWorld?wsdl</a>
will display the wsdl for this service</p>

<h1><a name="AsimpleJAX-WSservice-Accessingyourservice"></a>Accessing your
service</h1>

<p>and client code to see it working is at
<a href="http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/client/Client.java"
rel="nofollow">http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/client/Client.java</a></p>

<p>For the client there is also the alternative approach that gives you more flexibility.
Of course like above the logging interceptors are optional but they help a lot when starting:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JaxWsProxyFactoryBean factory = <span class="code-keyword">new</span> JaxWsProxyFactoryBean();
factory.getInInterceptors().add(<span class="code-keyword">new</span> LoggingInInterceptor());
factory.getOutInterceptors().add(<span class="code-keyword">new</span> LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress(<span class="code-quote">"http:<span class="code-comment">//localhost:9000/helloWorld"</span>);
</span>HelloWorld client = (HelloWorld) factory.create();

<span class="code-object">String</span> reply = client.sayHi(<span class="code-quote">"HI"</span>);
<span class="code-object">System</span>.out.println(<span class="code-quote">"Server
said: "</span> + reply);
<span class="code-object">System</span>.exit(0); 
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=60582&revisedVersion=27&originalVersion=26">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message