一、準備環境
IntelliJ IDEA 8.12
apache-cxf-2.2.2.zip
JDK 1.5
資源位址:
<a href="http://labs.xiaonei.com/apache-mirror/cxf/2.2.2/apache-cxf-2.2.2.zip">http://labs.xiaonei.com/apache-mirror/cxf/2.2.2/apache-cxf-2.2.2.zip</a>
使用向導建立一個工程cxfws1,将apache-cxf-2.2.2.zip解壓後裡面的lib下的包加入工程。
二、開發WebService服務端程式
package ws;
import javax.jws.WebService;
/**
* 定義服務接口
*
* @author leizhimin 2009-6-11 14:09:14
*/
@WebService
public interface HelloWorld {
String sayHello(String username);
}
* 實作服務接口
* @author leizhimin 2009-6-11 14:33:42
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String username) {
System.out.println("正在調用sayHello()方法...");
return "Hello " + username + "!";
}
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
* 服務端程式設定與啟動程式
* @author leizhimin 2009-6-11 14:41:23
public class HelloWorldServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorldImpl.class);
factory.setAddress("http://localhost:8080/service/HelloWorld");
Server server = factory.create();
server.start();
服務端程式之是以能這麼啟動,是因為cxf内置了開源的jetty伺服器。
三、開發WebService的用戶端
package client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import ws.HelloWorld;
* 用戶端調用代碼
* @author leizhimin 2009-6-11 14:46:45
public class TestClient {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloWorld.class);
HelloWorld helloWorld = (HelloWorld) factory.create();
String msg = helloWorld.sayHello("World");
System.out.println(msg);
四、測試
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://ws/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://ws/"xmlns:tns="http://ws/" 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>
</xs:complexType>
- <xs:complexType name="sayHelloResponse">
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:schema>
</wsdl:types>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello" />
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
</wsdl:output>
</wsdl:binding>
- <wsdl:service name="HelloWorldImplService">
- <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/service/HelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
說明服務釋出成功了。。
2、運作用戶端程式
Hello World!
Process finished with exit code 0
可見,測試圓滿成功!
本文轉自 leizhimin 51CTO部落格,原文連結:http://blog.51cto.com/lavasoft/165940,如需轉載請自行聯系原作者