使用Apache CXF和Spring集成创建Web Service

1.创建HelloWorld 接口类

<textarea cols="50" rows="15" name="code" class="java">package com.googlecode.garbagecan.cxfstudy.helloworld; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface HelloWorld { public @WebResult(name=&quot;String&quot;)String sayHi(@WebParam(name=&quot;text&quot;) String text); }</textarea>

 

2.创建HelloWorld实现类

<textarea cols="50" rows="15" name="code" class="java">package com.googlecode.garbagecan.cxfstudy.helloworld; public class HelloWorldImpl implements HelloWorld { public String sayHi(String name) { String msg = &quot;Hello &quot; + name + &quot;!&quot;; System.out.println(&quot;Server: &quot; + msg); return msg; } }</textarea>

 

3.修改web.xml文件

<textarea cols="50" rows="15" name="code" class="xhtml">&lt;!DOCTYPE web-app PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot; &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot; &gt; &lt;web-app&gt; &lt;display-name&gt;cxfstudy&lt;/display-name&gt; &lt;servlet&gt; &lt;servlet-name&gt;cxf&lt;/servlet-name&gt; &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;cxf&lt;/servlet-name&gt; &lt;url-pattern&gt;/ws/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath*:**/spring.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;/web-app&gt;</textarea>

 

4.创建spring配置文件并放在classpath路径下

<textarea cols="50" rows="15" name="code" class="xhtml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:jaxws=&quot;http://cxf.apache.org/jaxws&quot; xsi:schemaLocation=&quot;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&quot;&gt; &lt;import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; /&gt; &lt;import resource=&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot; /&gt; &lt;import resource=&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot; /&gt; &lt;jaxws:endpoint id=&quot;helloworld&quot; implementor=&quot;com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl&quot; address=&quot;/HelloWorld&quot; /&gt; &lt;!-- For client test --&gt; &lt;jaxws:client id=&quot;helloworldClient&quot; address=&quot;http://localhost:9000/ws/HelloWorld&quot; serviceClass=&quot;com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld&quot; /&gt; &lt;/beans&gt;</textarea>

 

5.创建测试类

<textarea cols="50" rows="15" name="code" class="java">package com.googlecode.garbagecan.cxfstudy.helloworld; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(&quot;spring.xml&quot;); HelloWorld helloworld = (HelloWorld)context.getBean(&quot;helloworldClient&quot;); System.out.println(helloworld.sayHi(&quot;kongxx&quot;)); } }</textarea>

 

6.测试

6. 1.首先启动tomcat或者使用maven的jetty,并访问http://localhost:9000/ws/HelloWorld?wsdl来验证web service已经启动并且生效;
6. 2.然后运行测试类来验证web service。

你可能感兴趣的:(使用Apache CXF和Spring集成创建Web Service)