首页 > 代码库 > Apache CXF之初探
Apache CXF之初探
一、到官网 http://cxf.apache.org/download.html 下载对应的 包。
二、新建Java 工程,把对应的jar包放入去。
三、创建 server 端的程序。
共有3个Java文件:
3.1 新建接口 HelloWorld,此接口只有一个方法,如下代码:
package com.yao.cxf.server; public interface HelloWorld { String sayHi(String text); }
package com.yao.cxf.server; public class HelloWorldImpl implements HelloWorld { @Override public String sayHi(String text) { return "Hello,"+text; } }
package com.yao.cxf.server; import org.apache.cxf.frontend.ServerFactoryBean; public class Server { public Server() throws Exception{ HelloWorldImpl hw = new HelloWorldImpl(); ServerFactoryBean sfb = new ServerFactoryBean(); sfb.setServiceClass(HelloWorld.class); sfb.setServiceBean(hw); sfb.setAddress("http://localhost:9000/Hello"); sfb.create(); } public static void main(String[] args) throws Exception{ new Server(); System.out.println("server start ..."); Thread.sleep(5*60*1000); System.out.println("server exit ..."); System.exit(0); } }
四、创建客户端:
客户端设置和服务器端一样的地址才可以访问:
package com.yao.cxf.client; import org.apache.cxf.frontend.ClientProxyFactoryBean; import com.yao.cxf.server.HelloWorld; public class Client { public static void main(String[] args) { ClientProxyFactoryBean clientFactory = new ClientProxyFactoryBean(); clientFactory.setAddress("http://localhost:9000/Hello"); HelloWorld hw = clientFactory.create(HelloWorld.class); System.out.println(hw.sayHi("yaokj")); } }
5.1 运行Server类打印如下的结果:
2014-8-20 15:28:15 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://server.cxf.yao.com/}HelloWorld from class com.yao.cxf.server.HelloWorld 2014-8-20 15:28:16 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server‘s publish address to be http://localhost:9000/Hello 2014-8-20 15:28:16 org.eclipse.jetty.server.Server doStart 信息: jetty-8.1.15.v20140411 2014-8-20 15:28:16 org.eclipse.jetty.server.AbstractConnector doStart 信息: Started SelectChannelConnector@localhost:9000 2014-8-20 15:28:16 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl 2014-8-20 15:28:16 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server‘s publish address to be soap.udp://239.255.255.250:3702 2014-8-20 15:28:16 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl server start ... server exit ...
2014-8-20 15:28:38 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://server.cxf.yao.com/}HelloWorld from class com.yao.cxf.server.HelloWorld Hello,yaokj
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。