[CXF]WebService框架CXF

**

WebService-CXF

**
什么是CXF
Apache CXF = Celtix + Xfire
支持多种协议:
SOAP1.1,1.2
XML/HTTP
CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)

并可以与Spring进行快速无缝的整合
灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。
在这里插入图片描述
在这里插入图片描述

CXF的结构:
在这里插入图片描述
下载完成后将其解压到某个目录下:
在这里插入图片描述

基于jdk1.7发布一个WebService服务
第一步:创建一个Java项目
第二步:创建一个类,加入Webservice注解
第三步:提供一个方法sayHello
第四步:在main方法中调用jdk提供的发布服务的方法
第五步:访问服务的wsdl文档(服务的发布地址+?wsdl)http://192.168.115.87:8080/hello?wsdl

@WebService
public class HelloService {
	public String sayHello(String name,int i){
		System.out.println("服务端的sayHello方法被调用了。。。。");
		return "helle" + name;
	}
	
	public static void main(String[] args) {
		String address = "http://192.168.115.87:8080/hello";
		Object implementor = new HelloService();
		Endpoint.publish(address, implementor);
	}
}

1客户端调用
1.jdk中wsimport命令使用
作用:解析wsdl文件,生成客户端本地代码
在这里插入图片描述
2客户端调用
1、使用wsimport命令解析wsdl文件生成本地代码
2、通过本地代码创建一个代理对象
3、通过代理对象实现远程调用

/**
 * 1、使用wsimport命令解析wsdl文件生成本地代码
 * 2、通过本地代码创建一个代理对象
 * 3、通过代理对象实现远程调用
 * @author zhaoqx
 *
 */
public class App {
	public static void main(String[] args) {
		HelloServiceService ss = new HelloServiceService();
		//创建客户端代理对象,用于远程调用
		HelloService proxy = ss.getHelloServicePort();
		String ret = proxy.sayHello("小明", 10);
		System.out.println(ret);
	}
}

4.2入门案例(服务端开发)
第一步:创建动态web项目
第二步:导入CXF相关jar包

第三步:在web.xml中配置CXF框架提供的一个Servlet

cxf org.apache.cxf.transport.servlet.CXFServlet config-location classpath:cxf.xml cxf /service/*

第四步:在类路径下提供cxf.xml

<?xml version="1.0" encoding="UTF-8"?>





第五步:开发一个接口和实现类

第六步:在cxf.xml中注册服务

服务的访问地址wsdl:

4.3入门案例(客户端开发)
方式一:使用jdk提供的wsimport命令生成本地代码完成调用

方式二:使用CXF提供的方式(重点)
第一步:创建Java项目并导入CXF相关jar包
第二步:使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件

第三步:将接口文件复制到项目中

第四步:提供spring配置文件,注册客户端代理对象

第五步:读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用

5基于CXF开发crm服务
5.1数据库环境搭建

执行sql脚本:

5.2web项目环境搭建
第一步:创建动态web项目
第二步:导入CXF相关jar包
第三步:配置web.xml

contextConfigLocation
classpath:cxf.xml

org.springframework.web.context.ContextLoaderListener cxf org.apache.cxf.transport.servlet.CXFServlet cxf /service/*

第四步:在类路径下提供cxf.xml

<?xml version="1.0" encoding="UTF-8"?>






第五步:针对t_customer表创建一个Customer客户实体类

第六步:开发一个接口和实现类

第七步:配置cxf.xml






<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 支持事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="customerService" class="com.itheima.crm.service.CustomerServiceImpl">
	<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>

<!-- 注册服务 -->
<jaxws:server id="myService" address="/customer">
	<jaxws:serviceBean>
		<ref bean="customerService"/>
	</jaxws:serviceBean>
</jaxws:server>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章