天天看点

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,如需转载请自行联系原作者