第一个webService的实例

说明:
先下载apache-cxf-2.2.3.zip;
下载路径: http://apache.etoak.com/cxf/2.2.3/apache-cxf-2.2.3.zip
把   \apache-cxf-2.2.3\apache-cxf-2.2.3\lib下jar文件导入到项目中;
 
下面是基本的文件:
 
1.接口文件:
package javaws;

import javax.jws.WebService;

@WebService
public interface HelloWorld
{
public String SayHello(String name);
}
 
2.实现接口文件:
package javaws;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld
{

   public String SayHello(String name)
  {
    System. out.println( "sevice is called!");    
     return "hello ,"+name;
  }

}
 
3.服务器端:
package javaws;
import org.apache.cxf.frontend.ServerFactoryBean;

public class MainServer
{
   public static void main(String[] args)    
  {
    
     HelloWorldImpl helloworldImpl = new HelloWorldImpl();
     ServerFactoryBean svrFactory = new ServerFactoryBean();
     svrFactory.setServiceClass(HelloWorld. class);
     svrFactory.setAddress( "http://localhost:8888/Hello");
     svrFactory.setServiceBean(helloworldImpl);
     svrFactory.create();    
            
  }

}
4.客户端:
package javaws;
import org.apache.cxf.frontend.ClientProxyFactoryBean;

public class TestClient
{

   public static void main(String[] args)
  {
    
     ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
     factory.setServiceClass(HelloWorld. class);
     factory.setAddress( "http://localhost:8888/Hello");
     HelloWorld client = (HelloWorld)factory.create();
     System.out.println("Invoke sayHi()....");
     System.out.println(client.SayHello("tiger"));
    
  }

}
 
 
运行时要先启动服务器端,在运行客户端,就可以看到调用服务的效果了。
这只是入门级的体验,记录备查,有待深入学习!
 

你可能感兴趣的:(webservice,职场,实例,休闲)