CXF和spring整合實現webservice實例

環境:JDK6.0,cxf-2.0.7,spring2.5,tomcat6.0

[b][color=red]服務器端[/color][/b]:

1.新建Web項目,例如webws,導入cxf-2.0.7/lib下的jar包和spring2.5的包,因爲cxf支持spring,因此它自帶有sping的一些核心包,爲了以後擴展,保險起見都一起導入吧。。

2.新建一個服務接口IHelloWorld.java,該接口是面向客戶端,暴露服務:
package cn.com.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {
//@WebParam(name="arg0")可有可無,爲了增強可讀性
public String sayHello(@WebParam(name="arg0")String text);
}

3.再新建該接口的實現類HelloWorldImpl.java:
package cn.com.service;
import javax.jws.WebService;

@WebService(endpointInterface="cn.com.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
public String sayHello(String text) {
return "Hello" + text ;
}
}

4.現在可以進行spring配置了,在eclipse的src文件夾下新建applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="hello" class="cn.com.service.HelloWorldImpl"/>

<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

</beans>

注意:1).<beans>元素裏面的命名空間一定要正確,特別要增加xmlns:jaxws="http://cxf.apache.org/jaxws",http://cxf.apache.org/jaxws,http://cxf.apache.org/schemas/jaxws.xsd;
2).cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三個文件都在cxf-2.0.7.jar中把它們拷貝到META-INF/目錄下。

5.配置web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

到此服務器端基本上告一段落,可以將應用部署到tomcat,啓動並訪問http://localhost:8080/webws/HelloWorld?wsdl,如果能正確顯示xml文件則說明部署成功。

[b][color=red]客戶端[/color][/b]:

1.新建一個java項目,例如wsclient,也導入cxf-2.0.7和spring包
2.新建客戶程序(如果熟悉cxf可以運用cxf的wsdl2java命令直接根據wsdl生成),如Test.java:

package pro.webws.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
IHelloWorld client = (IHelloWorld) ctx.getBean("client");
String result = client.sayHello("你好!");
System.out.println(result);
}
}

根據代碼,我們知道還需要新建一個IHelloWorld接口,這是服務器端暴露的服務,我們還需要在客戶端再新建一個以利用之,代碼和服務器端一致即可,不再贅述。

現在可以配置客戶端的spring文件了,新建spring-client.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd">

<bean id="client" class="pro.webws.client.IHelloWorld" factory-bean="clientFactory"
factory-method="create" />

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="pro.webws.client.IHelloWorld" />
<property name="address" value="http://localhost:8080/webws/HelloWorld" />
</bean>
</beans>

注意:<property name="address" value="http://localhost:8080/webws/HelloWorld" />中的/HelloWorld要與服務器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address屬性對應。

現在服務器和客戶端都已完成,啓動tomcat,運行客戶程序,將輸出結果:

Hello你好!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章