開發工具:MyEclipse 6.0
開發環境:
1. jdk1.5
2. CXF框架,版本apache-cxf-2.2.3.zip,到http://cxf.apache.org/download.html下載
注:如使用jdk1.6進行開發,需下載jaxb-api.jar和jaxws-api.jar,然后在本機安裝JDK的地方,在jdk1.6.0的jre文件夾下的lib文件夾中新建endorsed文件夾,放入以上兩個jar包才可以進行開發。
第一步,先在MyEclipse新建一個java項目,項目名為HelloWebService。
第二步,在項目中引入apache-cxf-2.2.3.zip中lib下的所有jar包。
第三步,編寫測試用接口以及其實現類:
接口:
- package test;
-
- import javax.jws.WebService;
-
- public interface Hello {
-
- public String sayHello(String str);
-
- }
package test;
import javax.jws.WebService;
public interface Hello {
public String sayHello(String str);
}
實現類:
- package test;
-
- public class HelloImpl implements Hello {
-
- public String sayHello(String str) {
-
- System.out.println("調用成功");
- return "Hello " + str;
- }
-
- }
package test;
public class HelloImpl implements Hello {
public String sayHello(String str) {
System.out.println("調用成功");
return "Hello " + str;
}
}
在接口中添加WebService的注解,將其標注為WebService的服務接口。
- @WebService
- public interface Hello {
@WebService
public interface Hello {
第四步,編寫WebService的服務器端。
- package test;
-
- import org.apache.cxf.endpoint.Server;
- import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
-
- public class MainServer {
-
- public static void main(String[] args) {
- JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
- factory.setAddress("http://localhost:8080/HelloWebService");
- factory.setServiceClass(HelloImpl.class);
- Server server = factory.create();
- server.start();
- }
- }
package test;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setAddress("http://localhost:8080/HelloWebService");
factory.setServiceClass(HelloImpl.class);
Server server = factory.create();
server.start();
}
}
factory.setAddress("http://localhost:8080/HelloWebService");
設置服務在服務器上部署的位置
factory.setServiceClass(HelloImpl.class);
設置服務暴露的接口實現類
完成之后運行MainServer中的main方法。
注:因為CXF框架中有Jetty 6 服務器,所以這個的demo發布在其中運行。
之后打開瀏覽器,輸入:
http://localhost:8080/HelloWebService?wsdl
如能看見以下畫面則WebService發布成功:
- <?xml version="1.0" encoding="UTF-8" ?>
- - <wsdl:definitions name="HelloImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- - <wsdl:types>
- - <xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0" xmlns:tns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="sayHello" type="tns:sayHello" />
- <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- - <xs:complexType name="sayHello">
- - <xs:sequence>
- <xs:element minOccurs="0" name="arg0" type="xs:string" />
- </xs:sequence>
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="HelloImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0" xmlns:tns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xs:complexType name="sayHello">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
</xs:sequence>
第五步,編寫客戶端
- package test;
-
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
-
- public class Client {
- public static void main(String[] args) {
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setServiceClass(Hello.class);
- factory.setAddress("http://localhost:8080/HelloWebService");
- Hello hello = (Hello)factory.create();
-
- System.out.println(hello.sayHello("weberyb"));
- }
-
- }
package test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/HelloWebService");
Hello hello = (Hello)factory.create();
System.out.println(hello.sayHello("weberyb"));
}
}
factory.setServiceClass(Hello.class);
設置訪問服務器端的指定接口。
factory.setAddress("http://localhost:8080/HelloWebService");
設置訪問的服務的地址。
factory.create()
創建代理對象以供遠程調用
之后運行Client中main方法,可以在控制臺的服務器端看見如下輸出:
- ....................
- 2009-8-13 14:00:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
- 信息: Creating Service {http://test/}HelloService from class test.Hello
- Hello weberyb
....................
2009-8-13 14:00:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://test/}HelloService from class test.Hello
Hello weberyb
說明客戶端調用WebService成功。
至此,這個簡單的
WebService開發完畢
嘗試過jdk1.6 它的包會變成import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean; 并且沒有factory.setAddress這個方法 采用自帶的例子進行測試 會出現參數null錯誤,但是客戶端調用服務器端都調用成功,可能原因是沒有加載那兩個JAR文件的問題,暫時沒有測試