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 Thu, 30 Aug 2012 19:15: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/A+simple+JAX-WS+service">A
simple JAX-WS service</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>
        Referring people to Maven for setup.<br />
    </div>
        <br/>
                         <h4>Changes (8)</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> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">This
example corresponds to the hello_world_code_first example in the CXF distribution. *IMPORTANT*:
This sample is only in CXF 2.0.1+! <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">This
example corresponds to the [java_first_jaxws|http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/]
example in the CXF distribution. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h1. Setting up your build
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">If
you&#39;re using Maven to build your project have a look at [this page|Maven Integration
and Plugin]. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
use of [Apache Maven|http://maven.apache.org/] is recommended for your web service projects,
as it will automatically bring in all necessary dependencies for your web service project.
 See the Maven [pom.xml|http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/pom.xml?view=co&amp;revision=1373526&amp;content-type=text%2Fplain]
for this sample for the configuration needed.  All samples provided by CXF use Apache Maven,
except for the [antbuild sample|http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/antbuild/]
which shows how you can build CXF projects with Apache Ant instead. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">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) <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
mvn dependency:list and mvn dependency:tree commands from the [Maven Dependency Plugin|http://maven.apache.org/plugins/maven-dependency-plugin/]
will show all dependencies used by your project. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{code}
<br>commons-logging-1.1.1.jar <br>geronimo-activation_1.1_spec-1.0.2.jar (or Sun&#39;s
Activation jar) <br>geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250) <br>geronimo-javamail_1.4_spec-1.6.jar
(or Sun&#39;s JavaMail jar) <br>geronimo-servlet_2.5_spec-1.2.jar (or Sun&#39;s
Servlet jar) <br>geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181) <br>geronimo-jaxws_2.1_spec-1.0.jar
(or Sun&#39;s jaxws-api-2.1.jar) <br>geronimo-stax-api_1.0_spec-1.0.1.jar (or other
stax-api jar) <br>jaxb-api-2.1.jar <br>jaxb-impl-2.1.12.jar <br>jetty-6.1.21.jar
<br>jetty-util-6.1.21.jar <br>neethi-2.0.4.jar <br>saaj-api-1.3.jar <br>saaj-impl-1.3.2.jar
<br>wsdl4j-1.6.2.jar <br>wstx-asl-3.2.8.jar <br>XmlSchema-1.4.5.jar <br>xml-resolver-1.2.jar
<br>{code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
Spring jars (optional - for XML Configuration support): <br>{code} <br>aopalliance-1.0.jar
<br>spring-core-2.5.5.jar <br>spring-beans-2.5.5.jar <br>spring-context-2.5.5.jar
<br>spring-web-2.5.5.jar <br>{code} <br> <br>And the CXF jar: <br>{code:java}
<br>cxf-2.2.3.jar <br>{code} <br> <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 {{sayHi}} which
says &quot;Hello&quot; to whoever submits their name. <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 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 <a href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/"
class="external-link" rel="nofollow">java_first_jaxws</a> example in the CXF distribution.</p>

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

<p>The use of <a href="http://maven.apache.org/" class="external-link" rel="nofollow">Apache
Maven</a> is recommended for your web service projects, as it will automatically bring
in all necessary dependencies for your web service project.  See the Maven <a href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/pom.xml?view=co&amp;revision=1373526&amp;content-type=text%2Fplain"
class="external-link" rel="nofollow">pom.xml</a> for this sample for the configuration
needed.  All samples provided by CXF use Apache Maven, except for the <a href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/antbuild/"
class="external-link" rel="nofollow">antbuild sample</a> which shows how you can
build CXF projects with Apache Ant instead.</p>

<p>The mvn dependency:list and mvn dependency:tree commands from the <a href="http://maven.apache.org/plugins/maven-dependency-plugin/"
class="external-link" rel="nofollow">Maven Dependency Plugin</a> will show all dependencies
used by your project.</p>


<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 <tt>sayHi</tt>
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/main/java/demo/hw/server/Server.java"
class="external-link" rel="nofollow">http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/main/java/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" class="external-link"
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/main/java/demo/hw/client/Client.java"
class="external-link" rel="nofollow">http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/main/java/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="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=60582&revisedVersion=32&originalVersion=31">View
Changes</a>
                |
        <a href="https://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