You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2017/10/15 19:57:09 UTC

svn commit: r1019553 - in /websites/production/cxf/content: cache/docs.pageCache docs/writing-a-service-with-spring.html

Author: buildbot
Date: Sun Oct 15 19:57:09 2017
New Revision: 1019553

Log:
Production update by buildbot for cxf

Modified:
    websites/production/cxf/content/cache/docs.pageCache
    websites/production/cxf/content/docs/writing-a-service-with-spring.html

Modified: websites/production/cxf/content/cache/docs.pageCache
==============================================================================
Binary files - no diff available.

Modified: websites/production/cxf/content/docs/writing-a-service-with-spring.html
==============================================================================
--- websites/production/cxf/content/docs/writing-a-service-with-spring.html (original)
+++ websites/production/cxf/content/docs/writing-a-service-with-spring.html Sun Oct 15 19:57:09 2017
@@ -28,6 +28,16 @@
 <meta name="description" content="Apache CXF, Services Framework - Writing a service with Spring">
 
 
+<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shCoreCXF.css">
+<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shThemeCXF.css">
+
+<script src='/resources/highlighter/scripts/shCore.js'></script>
+<script src='/resources/highlighter/scripts/shBrushJava.js'></script>
+<script src='/resources/highlighter/scripts/shBrushXml.js'></script>
+<script>
+  SyntaxHighlighter.defaults['toolbar'] = false;
+  SyntaxHighlighter.all();
+</script>
 
 
     <title>
@@ -107,16 +117,168 @@ Apache CXF -- Writing a service with Spr
          <td height="100%">
            <!-- Content -->
            <div class="wiki-content">
-<div id="ConfluenceContent"><p>This example will lead you through creating your first service with <a shape="rect" class="external-link" href="http://springframework.org" 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 <a shape="rect" class="external-link" href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/">java_first_spring_support</a> example in the CXF distribution.</p><h1 id="WritingaservicewithSpring-Settingupyourbuild">Setting up your build</h1><p>The use of <a shape="rect" class="external-link" href="http://maven.apache.org/">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 shape="rect" class="external-link" href="http://svn.apache.org/viewvc/cxf
 /trunk/distribution/src/main/release/samples/java_first_spring_support/pom.xml?view=co">pom.xml</a> for this sample for the configuration needed. All samples provided by CXF use Apache Maven, except for the <a shape="rect" class="external-link" href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/antbuild/">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 shape="rect" class="external-link" href="http://maven.apache.org/plugins/maven-dependency-plugin/">Maven Dependency Plugin</a> will show all dependencies used by your project.</p><h1 id="WritingaservicewithSpring-WritingyourService">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>
-<p>Our implementation will then look like this:</p>
-<p>The <code>@WebService</code> 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 id="WritingaservicewithSpring-Declaringyourserverbeans">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>
-<p>If you want to reference a spring managed-bean, you can write like this:</p><p>xml</p>
- ]]&gt;<p>The bean uses the following properties:</p><ul><li><code>id</code> specifies the id of the bean in the Spring context.</li><li><code>implementor</code> specifies the implementation class.</li><li><code>address</code> 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 <code>&lt;jaxws:endpoint&gt;</code> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service. For more on this see <a shape="rect" href="jax-ws-configuration.html">JAX-WS Configuration</a>.</p><h1 id="WritingaservicewithSpring-SettinguptheServlet">Setting up the Servlet</h1><p>
 Since we're relying on the default "cxf-servlet.xml" file a <a shape="rect" rel="nofollow">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 <code>ContextLoaderLister</code>. This starts Spring and explicitly loads the configuration file. We can specify where our file is via a <code>context-param</code> element.</li></ol><p>An example:</p><p>java</p>
- ...contextConfigLocationWEB-INF/beans.xml org.springframework.web.context.ContextLoaderListener ]]&gt;<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 id="WritingaservicewithSpring-CreateaClient(EasyWay)">Create a Client (Easy Way)</h1><p>Just like the <code>&lt;jaxws:endpoint&gt;</code> used on the server side, there is a <code>&lt;jaxws:client&gt;</code> 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><p>xml</p>
-]]&gt;<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><p>java</p>
-<p>You can also do more sophisticated things with the <code>&lt;jaxws:client&gt;</code> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client. For more on this see <a shape="rect" href="jax-ws-configuration.html">JAX-WS Configuration</a>.</p><h1 id="WritingaservicewithSpring-CreateaClient(MoreManualWay)">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>
-<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><p>java</p>
-<p>client code at <a shape="rect" class="external-link" href="https://github.com/apache/cxf/blob/master/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/client/Client.java" rel="nofollow">https://github.com/apache/cxf/blob/master/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/client/Client.java</a></p><p>Some usage scenarios will require more extensive configuration (and this is not the case with the <code>&lt;jaxws:client&gt;</code> syntax described above). For more information, see <a shape="rect" href="jax-ws-configuration.html">JAX-WS Configuration</a>.</p><h1 id="WritingaservicewithSpring-AdvancedSteps">Advanced Steps</h1><p>For more information on using Spring you may want to read the <a shape="rect" href="configuration.html">Configuration</a> and <a shape="rect" href="spring.html">Spring</a> sections of the User's Guide.</p></div>
+<div id="ConfluenceContent"><p>This example will lead you through creating your first service with <a shape="rect" class="external-link" href="http://springframework.org" 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 <a shape="rect" class="external-link" href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/">java_first_spring_support</a> example in the CXF distribution.</p><h1 id="WritingaservicewithSpring-Settingupyourbuild">Setting up your build</h1><p>The use of <a shape="rect" class="external-link" href="http://maven.apache.org/">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 shape="rect" class="external-link" href="http://svn.apache.org/viewvc/cxf
 /trunk/distribution/src/main/release/samples/java_first_spring_support/pom.xml?view=co">pom.xml</a> for this sample for the configuration needed. All samples provided by CXF use Apache Maven, except for the <a shape="rect" class="external-link" href="http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/antbuild/">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 shape="rect" class="external-link" href="http://maven.apache.org/plugins/maven-dependency-plugin/">Maven Dependency Plugin</a> will show all dependencies used by your project.</p><h1 id="WritingaservicewithSpring-WritingyourService">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 pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl
 ">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+// START SNIPPET: service
+package demo.spring.service;
+
+import javax.jws.WebService;
+
+@WebService
+public interface HelloWorld {
+    String sayHi(String text);
+}
+// END SNIPPET: service
+</pre>
+</div></div><p>Our implementation will then look like this:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+// START SNIPPET: service
+package demo.spring.service;
+
+import javax.jws.WebService;
+
+@WebService(endpointInterface = "demo.spring.service.HelloWorld")
+public class HelloWorldImpl implements HelloWorld {
+
+    public String sayHi(String text) {
+        System.out.println("sayHi called");
+        return "Hello " + text;
+    }
+}
+// END SNIPPET: service
+</pre>
+</div></div><p>The <code>@WebService</code> 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 id="WritingaservicewithSpring-Declaringyourserverbeans">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 pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
+&lt;!--
+        Licensed to the Apache Software Foundation (ASF) under one
+        or more contributor license agreements. See the NOTICE file
+        distributed with this work for additional information
+        regarding copyright ownership. The ASF licenses this file
+        to you under the Apache License, Version 2.0 (the
+        "License"); you may not use this file except in compliance
+        with the License. You may obtain a copy of the License at
+        
+        http://www.apache.org/licenses/LICENSE-2.0
+        
+        Unless required by applicable law or agreed to in writing,
+        software distributed under the License is distributed on an
+        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+        KIND, either express or implied. See the License for the
+        specific language governing permissions and limitations
+        under the License.
+--&gt;
+&lt;!-- START SNIPPET: beans --&gt;
+&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"&gt;
+    &lt;import resource="classpath:META-INF/cxf/cxf.xml"/&gt;
+    &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml"/&gt;
+    &lt;jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/&gt;
+&lt;/beans&gt;
+&lt;!-- END SNIPPET: 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 pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">&lt;bean id="hello" class="demo.spring.service.HelloWorldImpl" /&gt;
+&lt;jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /&gt;
+</pre>
+</div></div><p>&#160;</p><p>The bean uses the following properties:</p><ul><li><code>id</code> specifies the id of the bean in the Spring context.</li><li><code>implementor</code> specifies the implementation class.</li><li><code>address</code> 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 <code>&lt;jaxws:endpoint&gt;</code> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service. For more on this see <a shape="rect" href="jax-ws-configuration.html">JAX-WS Configuration</a>.</p><h1 id="WritingaservicewithSpring-SettinguptheServlet">Setting up t
 he Servlet</h1><p>Since we're relying on the default "cxf-servlet.xml" file a <a shape="rect" rel="nofollow">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 <code>ContextLoaderLister</code>. This starts Spring and explicitly loads the configuration file. We can specify where our file is via a <code>context-param</code> element.</li></ol><p>An example:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">&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 id="WritingaservicewithSpring-CreateaClient(EasyWay)">Create a Client (Easy Way)</h1><p>Just like the <code>&lt;jaxws:endpoint&gt;</code> used on the server side, there is a <code>&lt;jaxws:client&gt;</code> 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 pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
+&lt;beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:jaxws="http://cxf.apache.org/jaxws"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       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="helloClient"
+                  serviceClass="demo.spring.HelloWorld"
+                  address="http://localhost:9002/HelloWorld" /&gt;
+&lt;/beans&gt;</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 pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">ApplicationContext context = ...; // your Spring ApplicationContext
+HelloWorld client = (HelloWorld) context.getBean("helloClient");
+</pre>
+</div></div><p>You can also do more sophisticated things with the <code>&lt;jaxws:client&gt;</code> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client. For more on this see <a shape="rect" href="jax-ws-configuration.html">JAX-WS Configuration</a>.</p><h1 id="WritingaservicewithSpring-CreateaClient(MoreManualWay)">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="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
+&lt;!--
+        Licensed to the Apache Software Foundation (ASF) under one
+        or more contributor license agreements. See the NOTICE file
+        distributed with this work for additional information
+        regarding copyright ownership. The ASF licenses this file
+        to you under the Apache License, Version 2.0 (the
+        "License"); you may not use this file except in compliance
+        with the License. You may obtain a copy of the License at
+        
+        http://www.apache.org/licenses/LICENSE-2.0
+        
+        Unless required by applicable law or agreed to in writing,
+        software distributed under the License is distributed on an
+        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+        KIND, either express or implied. See the License for the
+        specific language governing permissions and limitations
+        under the License.
+--&gt;
+&lt;!-- START SNIPPET: beans --&gt;
+&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"&gt;
+    &lt;bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/&gt;
+    &lt;bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&gt;
+        &lt;property name="serviceClass" value="demo.spring.service.HelloWorld"/&gt;
+        &lt;property name="address" value="http://localhost:9002/services/HelloWorld"/&gt;
+    &lt;/bean&gt;
+&lt;/beans&gt;
+&lt;!-- END SNIPPET: beans --&gt;
+</pre>
+</div></div><p>&#160;</p><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 pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">ApplicationContext context = ...; // your Spring ApplicationContext
+HelloWorld client = (HelloWorld) context.getBean("client");
+</pre>
+</div></div><p>&#160;</p><p>client code at <a shape="rect" class="external-link" href="https://github.com/apache/cxf/blob/master/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/client/Client.java" rel="nofollow">https://github.com/apache/cxf/blob/master/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/client/Client.java</a></p><p>Some usage scenarios will require more extensive configuration (and this is not the case with the <code>&lt;jaxws:client&gt;</code> syntax described above). For more information, see <a shape="rect" href="jax-ws-configuration.html">JAX-WS Configuration</a>.</p><h1 id="WritingaservicewithSpring-AdvancedSteps">Advanced Steps</h1><p>For more information on using Spring you may want to read the <a shape="rect" href="configuration.html">Configuration</a> and <a shape="rect" href="spring.html">Spring</a> sections of the User's Guide.</p></div>
            </div>
            <!-- Content -->
          </td>