理論聯繫實際,單單隻講理論那就成了紙上談兵,用一個HelloWorld Demo可來說明事更加直觀。那下面咱們就開始進行講解:
首先到apache官方網下載apache-cxf-2.2.2,(現在有更高版本2.4)地址:http://cxf.apache.org/
新建一個Java Project,導入cxf常用.jar包,cxf常用jar包如下:
1、commons-logging-1.1.1.jar
2、cxf-2.4.1.jar
3、geronimo-activation_1.1_spec-1.1.jar
4、geronimo-annotation_1.0_spec-1.1.1.jar
5、geronimo-javamail_1.4_spec-1.7.1.jar
6、geronimo-jaxws_2.2_spec-1.0.jar
7、geronimo-servlet_3.0_spec-1.0.jar
8、geronimo-stax-api_1.0_spec-1.0.1.jar
9、geronimo-ws-metadata_2.0_spec-1.1.3.jar
10、jettison-1.3.jar
11、jetty-continuation-7.4.2.v20110526.jar
12、jetty-http-7.4.2.v20110526.jar
13、jetty-io-7.4.2.v20110526.jar
14、jetty-server-7.4.2.v20110526.jar
15、jetty-util-7.4.2.v20110526.jar
16、neethi-3.0.0.jar
17、saaj-api-1.3.jar
18、saaj-impl-1.3.2.jar
19、serializer-2.7.1.jar
cxf結合spring時所需jar包
(
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-aop-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
)
20、wsdl4j-1.6.2.jar
21、xalan-2.7.1.jar
22、xercesImpl.jar
23、xml-resolver-1.2.jar
24、xmlschema-core-2.0.jar
25、jaxb-api-2.2.1.jar ---- webservices服務端需要加
26、jaxb-impl-2.2.1.1.jar ---- 如果jdk中的版本與該版本一致,則webservices服務端和客戶端都不需要加
(注:jaxb-api和jaxb-impl包會和jdk中的衝突(jdk的版本<2.0>較低),在用這兩個jar包的時候,可將2.2版本的jar覆蓋jdk的版本)
覆蓋jdk版本的方法: 找到jdk的安裝目錄下的jre\lib\endorsed文件夾(如果endorsed文件夾不存在,可新建),將jaxb-api-2.2.1和jaxb-impl-2.2.1.1放到此文件夾下即可。
第一步:新建一個webservice服務端接口和實現類
1、服務端接口
package com.ws.services;
import javax.jws.WebService;
@WebService
public interface IHelloServices {
public String sayHelloToAll(String[] userNames);
public String[] getHelloWords();
public String sayHello(String name);
}
2、服務端接口實現類
package com.ws.services.impl;
import javax.jws.WebService;
import com.ws.services.IHelloServices;
@WebService(endpointInterface="com.ws.services.IHelloServices")
public class HelloServicesImpl implements IHelloServices {
public String[] getHelloWords() {
String[] words = {"hello vicky.","hello,i'm vicky.","hi,ivy and simon."};
return words;
}
public String sayHello(String name) {
return "hello "+name+" ! ";
}
public String sayHelloToAll(String[] userNames) {
String hello = "hello ";
for(int i=0;i<userNames.length;i++){
if(i!=userNames.length-1)
hello += userNames[i]+" and ";
else
hello += userNames[i]+" .";
}
return hello;
}
}
3、創建webservices服務端,併發布服務
package com.test;
import javax.xml.ws.Endpoint;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.ws.services.IHelloServices;
import com.ws.services.impl.HelloServicesImpl;
import com.ws.services.impl.UserServicesImpl;
public class ServerTest {
public ServerTest(){
// 第一種發佈方式
IHelloServices hello = new HelloServicesImpl();
// 創建WebServices服務接口
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 註冊webservices接口
factory.setServiceClass(IHelloServices.class);
// 發佈接口
factory.setAddress("http://localhost:8090/helloServices");
factory.setServiceBean(hello);
// 創建服務
factory.create();
// 第二種發佈方式
//Endpoint.publish("http://localhost:8090/helloServices", new HelloServicesImpl());
}
public static void main(String[] args) {
// 啓動服務
new ServerTest();
System.out.println("Server ready...");
try {
Thread.sleep(1000*300); //休眠五分分鐘,便於測試
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Server exit...");
System.exit(0);
}
}
第二步:新建一個webservice客戶端,並測試webServices的服務
1、在本工程中測試(即服務端與客戶端在同一個工程中)
package com.ws.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.ws.server.IHelloServices;
public class HelloTest {
public static void main(String[] args) {
//創建WebService客戶端代理工廠
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//註冊WebService接口
factory.setServiceClass(IHelloServices.class);
//設置WebService地址
factory.setAddress("http://localhost:8090/helloServices");
IHelloServices iHelloWorld = (IHelloServices)factory.create();
System.out.println("invoke webservice...");
String[] hellos = iHelloWorld.getHelloWords();
for(int i=0;i<hellos.length;i++){
System.out.println(hellos[i]);
}
System.exit(0);
}
}
2、新建一個webservices客戶端測試工程
(1)、新建一個Project,並加上cxf的jar包;
(2)、將Webservices服務端工程中的接口類Copy到客戶端工程中,且路徑要一直;
(3)、新建一個測試類,代碼如上。
最後是萬事俱備,只欠測試了
首先,運行服務端程序
其次,打開瀏覽器,在地址欄中輸入http://localhost:8090/helloServices?wsdl(因爲cxf自帶了一個jetty服務器),查看接口是否發佈成功,如裏瀏覽器頁面顯示下面內容,證明接口發佈成功 。
最後,運行客戶程序。