天天看點

IDEA8實戰-CXF版HelloWorld

一、準備環境

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); 

四、測試

    &lt;?xml version="1.0" encoding="UTF-8" ?&gt;    

- &lt;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"&gt; 

- &lt;wsdl:types&gt; 

- &lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://ws/"xmlns:tns="http://ws/" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; 

    &lt;xs:element name="sayHello" type="tns:sayHello" /&gt;    

    &lt;xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /&gt;    

- &lt;xs:complexType name="sayHello"&gt; 

- &lt;xs:sequence&gt; 

    &lt;xs:element minOccurs="0" name="arg0" type="xs:string" /&gt;    

    &lt;/xs:sequence&gt; 

    &lt;/xs:complexType&gt; 

- &lt;xs:complexType name="sayHelloResponse"&gt; 

    &lt;xs:element minOccurs="0" name="return" type="xs:string" /&gt;    

    &lt;/xs:schema&gt; 

    &lt;/wsdl:types&gt; 

- &lt;wsdl:message name="sayHelloResponse"&gt; 

    &lt;wsdl:part element="tns:sayHelloResponse" name="parameters" /&gt;    

    &lt;/wsdl:message&gt; 

- &lt;wsdl:message name="sayHello"&gt; 

    &lt;wsdl:part element="tns:sayHello" name="parameters" /&gt;    

- &lt;wsdl:portType name="HelloWorld"&gt; 

- &lt;wsdl:operation name="sayHello"&gt; 

    &lt;wsdl:input message="tns:sayHello" name="sayHello" /&gt;    

    &lt;wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /&gt;    

    &lt;/wsdl:operation&gt; 

    &lt;/wsdl:portType&gt; 

- &lt;wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"&gt; 

    &lt;soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /&gt;    

    &lt;soap:operation soapAction="" style="document" /&gt;    

- &lt;wsdl:input name="sayHello"&gt; 

    &lt;soap:body use="literal" /&gt;    

    &lt;/wsdl:input&gt; 

- &lt;wsdl:output name="sayHelloResponse"&gt; 

    &lt;/wsdl:output&gt; 

    &lt;/wsdl:binding&gt; 

- &lt;wsdl:service name="HelloWorldImplService"&gt; 

- &lt;wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"&gt; 

    &lt;soap:address location="http://localhost:8080/service/HelloWorld" /&gt;    

    &lt;/wsdl:port&gt; 

    &lt;/wsdl:service&gt; 

    &lt;/wsdl:definitions&gt;

說明服務釋出成功了。。

2、運作用戶端程式

Hello World! 

Process finished with exit code 0

可見,測試圓滿成功!

本文轉自 leizhimin 51CTO部落格,原文連結:http://blog.51cto.com/lavasoft/165940,如需轉載請自行聯系原作者