apache cxf ws demo

来源:互联网 发布:域名注册信息批量查询 编辑:程序博客网 时间:2024/05/16 00:59

Apache的cxf 的WebService

服务端:

1.创建HelloWorld 接口类


package com.cxf.webService;import javax.jws.WebService;@WebServicepublic interface HelloWorld {    public String sayHi(String text);}


2.创建HelloWorld  实现类


package com.cxf.webService.impl;import javax.jws.WebService;import com.cxf.webService.HelloWorld;@WebService(endpointInterface="com.cxf.webService.HelloWorld",serviceName="reservationService")public class HelloWorldImpl implements HelloWorld {public String sayHi(String text) {return "Hello:" + text;}}


3.创建Client端测试类


package com.cxf.test;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  public class Client {      public static void main(String[] args) {          JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();          factory.setServiceClass(HelloWorld.class);          factory.setAddress("http://localhost:9000/ws/HelloWorld");          HelloWorld helloworld = (HelloWorld) factory.create();          System.out.println(helloworld.sayHi("kongxx"));          System.exit(0);      }  } 


4.创建Server端测试类


package com.cxf.test;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  public class Server {      public static void main(String[] args) throws Exception {          JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();          factory.setServiceClass(HelloWorldImpl.class);                    factory.setAddress("http://localhost:9000/ws/HelloWorld");          factory.create();            System.out.println("Server start...");          Thread.sleep(60 * 1000);          System.out.println("Server exit...");          System.exit(0);      }  }


5.测试

首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。 运行
Client测试类,会在命令行输出Hello kongxx!的message。


0 0
原创粉丝点击