WebService CXF学习(入门篇2):HelloWorld示例
?理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloWorld Demo可来说明事更加直观。那下面咱们就开始进行讲解:
第一步:新建一个webservice服务端接口和实现类 ?1、服务端接口 package com.ws.services; import javax.jws.WebService; @WebService public interface IHelloServices { public String sayHelloToAll(String[] userNames); public String[] getHelloWords(); public String sayHello(String name); } ? 2、服务端接口实现类 package com.ws.services.impl; import javax.jws.WebService; import com.ws.services.IHelloServices; @WebService(endpointInterface="com.ws.services.IHelloServices") public class HelloServicesImpl implements IHelloServices { public String[] getHelloWords() { String[] words = {"hello vicky.","hello,i'm vicky.","hi,ivy and simon."}; return words; } public String sayHello(String name) { return "hello "+name+" ! "; } public String sayHelloToAll(String[] userNames) { String hello = "hello "; for(int i=0;i<userNames.length;i++){ if(i!=userNames.length-1) hello += userNames[i]+" and "; else hello += userNames[i]+" ."; } return hello; } } ?3、创建webservices服务端,并发布服务 package com.test; import javax.xml.ws.Endpoint; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.ws.services.IHelloServices; import com.ws.services.impl.HelloServicesImpl; import com.ws.services.impl.UserServicesImpl; public class ServerTest { public ServerTest(){ // 第一种发布方式 IHelloServices hello = new HelloServicesImpl(); // 创建WebServices服务接口 JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); // 注册webservices接口 factory.setServiceClass(IHelloServices.class); // 发布接口 factory.setAddress("http://localhost:8090/helloServices"); factory.setServiceBean(hello); // 创建服务 factory.create(); // 第二种发布方式 //Endpoint.publish("http://localhost:8090/helloServices",new HelloServicesImpl()); } public static void main(String[] args) { // 启动服务 new ServerTest(); System.out.println("Server ready..."); try { Thread.sleep(1000*300); //休眠五分分钟,便于测试 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Server exit..."); System.exit(0); } }
第二步:新建一个webservice客户端,并测试webServices的服务 ??? 1、在本工程中测试(即服务端与客户端在同一个工程中) package com.ws.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.ws.server.IHelloServices; public class HelloTest { public static void main(String[] args) { //创建WebService客户端代理工厂 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //注册WebService接口 factory.setServiceClass(IHelloServices.class); //设置WebService地址 factory.setAddress("http://localhost:8090/helloServices"); IHelloServices iHelloWorld = (IHelloServices)factory.create(); System.out.println("invoke webservice..."); String[] hellos = iHelloWorld.getHelloWords(); for(int i=0;i<hellos.length;i++){ System.out.println(hellos[i]); } System.exit(0); } }
???? (1)、新建一个Project,并加上cxf的jar包; ???? (2)、将Webservices服务端工程中的接口类Copy到客户端工程中,且路径要一直; ???? (3)、新建一个测试类,代码如上。 最后是万事俱备,只欠测试了 ??? 首先,运行服务端程序 ??? 其次,打开浏览器,在地址栏中输入http://localhost:8090/helloServices?wsdl(因为cxf自带了一个jetty服务器),查看接口是否发布成功,如里浏览器页面显示下面内容,证明接口发布成功 。??? 最后,运行客户程序。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |