IDEA8实战-CXF版HelloWorld

IDEA8实战-CXF版HelloWorld
 
我看网上做WebService都用MyEclipse,现在把国人用户都封了,我干脆写个IDEA版的CXF 的WS的例子,与大家分享!
 
一、准备环境
 
IntelliJ IDEA 8.12
apache-cxf-2.2.2.zip
JDK 1.5
 
资源地址:
http://labs.xiaonei.com/apache-mirror/cxf/2.2.2/apache-cxf-2.2.2.zip
 
 
使用向导创建一个工程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);
}
 
package ws;

import javax.jws.WebService;

/**
* 实现服务接口
*
* @author leizhimin 2009-6-11 14:33:42
*/
@WebService
public class HelloWorldImpl implements HelloWorld {
         public String sayHello(String username) {
                System.out.println( "正在调用sayHello()方法...");
                 return "Hello " + username + "!";
        }
}
 
package ws;

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 {
         public static void main(String[] args) {
                JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
                factory.setAddress( "http://localhost:8080/service/HelloWorld");
                factory.setServiceClass(HelloWorld.class);
                HelloWorld helloWorld = (HelloWorld) factory.create();
                String msg = helloWorld.sayHello("World");
                System.out.println(msg);
        }
}
 
四、测试
 
1、运行服务端程序,并通过页面访问地址 http://localhost:8080/service/HelloWorld?wsdl,显示的WSDL代码如下:
     <? 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:sequence >
     < xs:element minOccurs ="0" name ="return" type ="xs:string" />    
     </ xs:sequence >
     </ xs:complexType >
     </ 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:message >
- < 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" />    
- < wsdl:operation name ="sayHello" >
     < soap:operation soapAction ="" style="document" />    
- < wsdl:input name ="sayHello" >
     < soap:body use ="literal" />    
     </ wsdl:input >
- < wsdl:output name ="sayHelloResponse" >
     < soap:body use ="literal" />    
     </ wsdl:output >
     </ wsdl:operation >
     </ 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
 
 
可见,测试圆满成功!
 

本文出自 “熔 岩” 博客,转载请与作者联系!

你可能感兴趣的:(CXF,职场,休闲)