分布式系统(四)---Web Service实战--CXF实践篇
来源:互联网 发布:excel时间自动刷新数据 编辑:程序博客网 时间:2024/06/02 14:17
第二篇:CXF实践篇
CXF架构开发WebService步骤:
1、建立Web项目
2、准备所有的jar包
3、web.xml中配置cxf的核心servlet,CXFServlet
服务器端:
<display-name>cxf_demo</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-server.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
4、applicationContext-Server.xml
服务器端
<import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="helloService" implementor="com.test.server.HelloWorldServerImpl"address="/helloService" />
客户端
<import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><bean id="client" class="com.test.server.IHelloWorldServer"factory-bean="clientFactory" factory-method="create" /><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="com.test.server.IHelloWorldServer" /><property name="address" value="http://localhost:8080/cxf_demo/ws/helloService"/></bean>
CXF发布服务的类有两个:
JaxWsServerFactoryBean,我们用的这个。用于发布一个服务,可以通过默认构造实例此类。
JaxRsServerFactoryBean,此类用于发布Restful风格的webService;Restful风格是以普通get,post请求为标准的,并可以请求和相应json数据。
5、代码
服务器端,发布服务
IHelloWorldServer
@WebServicepublic interface IHelloWorldServer {public String sayHello(String username);}
HelloWorldServerImpl
@WebService(endpointInterface = "com.test.server.IHelloWorldServer",serviceName="HelloService")public class HelloWorldServerImpl implements IHelloWorldServer{@Overridepublic String sayHello(String username) {return username + ":HelloWorld";}}
客户端
HelloWorldClient
public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");IHelloWorldServer helloService = (IHelloWorldServer) context.getBean("client");String response = helloService.sayHello("liutengteng");System.out.println(response);}
6、运行结果
访问地址:http://localhost:8080/cxf_demo/ws
WSDL:
客户端运行结果:
总结
通过上面简单的例子我们也很容易的看出来,远程调用就是通过服务器端发布服务,客户端调用。发布出来的WSDL通过XML的形式展示出来,XML解析,而且SOAP也是基于XML的。由于XML是各种语言通用的,故Web Service实现了跨平台,跨语言。
1 1
- 分布式系统(四)---Web Service实战--CXF实践篇
- 分布式系统系列(三)---Web Service实战--CXF理论篇
- Apache CXF实战之四 构建RESTful Web Service
- Apache CXF实战之四 构建RESTful Web Service
- Apache CXF实战之四 构建RESTful Web Service
- Apache CXF实战之四 构建RESTful Web Service
- Apache CXF实战之四:构建RESTful Web Service
- Apache CXF实战之四 构建RESTful Web Service
- Apache CXF实战之四 构建RESTful Web Service
- 实战Web Service with CXF
- Web Service (四) 手动发布Web Service接口-CXF与Spring集成(实战)
- cxf+web service(四)cxf+spring集成
- 使用CXF开发web service 的客户端 (四)
- Apache CXF实战 构建RESTful Web Service
- Web Service笔记(五):CXF开发RESTful风格的Web Service(实战)
- Web Service笔记(四):利用CXF结合Spring开发web service
- Web service学习cxf版(四)使用cxf处理Map类型----昊哥
- Apache CXF实战之一 Hello World Web Service
- DWR3.0框架入门(1) —— 实现ajax
- 华为USG2230E sslvpn
- Jetty与tomcat的比较
- 系统入门(3): Android中的Parcel是什么
- jQuery ajax - ajax() 方法
- 分布式系统(四)---Web Service实战--CXF实践篇
- 代码实现退出应用
- 1003. 我要通过
- DWR3.0框架入门(2) —— DWR的服务器推送
- window对象
- 浅谈mybatis优缺点
- tmp study note
- JAVA 可变参数的定义 String ... ids
- 单片机和虚拟机Linux双向通信