webService框架cxf的简单使用

一、服务端提供服务

1、pom文件中导入cxf所需的jar包

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.0.1</version>

2、导入cxf.xml资源文件,需要在资源文件根目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://cxf.apache.org/bindings/soap 
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd
					http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">	
	<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
	<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" />
</beans>
3、在web.xml中配置cxf提供的servlet和资源文件位置
  <!-- 配置cxf提供的的servlet -->
  <servlet>
  	<servlet-name>cxf-servlet</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>cxf-servlet</servlet-name>
  	<url-pattern>/service/*</url-pattern>
  </servlet-mapping>
  	<!-- 通过初始化参数指定cxf框架的配置文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:cxf.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

4、创建接口并添加实现类,接口上需要有@WebService的注解。
@WebService
public interface ICustomerService {
	List<Customer> findAll();

}

public class CustomerServiceImpl implements ICustomerService {

	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	/**
	 * 查询所有客户
	 */
	public List<Customer> findAll() {
		String sql = "select * from t_customer";
		List<Customer> listCustomer = jdbcTemplate.query(sql, new RowMapper<Customer>(){
			public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String station = rs.getString("station");
				String telephone = rs.getString("telephone");
				String address = rs.getString("address");
				String decidedzone_id = rs.getString("decidedzone_id");
				Customer customer = new Customer(id, name, station, telephone, address, decidedzone_id);
				return customer;
			}
		});
		return listCustomer;
	}

}

5、在cxf.xml中注册cxf服务

	<!-- 注册服务 -->
	<bean id="customer" class="com.itheima.bos_crm.service.CustomerServiceImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<jaxws:server id="myService" address="/customer">
		<jaxws:serviceBean><ref bean="customer"></ref></jaxws:serviceBean>
	</jaxws:server>

客户端访问的地址为:ip:端口号/项目名称/web.xml中servlet配置的service/cxf.xml中注册的customer


二、客户端使用服务

1、创建项目并且导入cxf的jar包

2、使用jdk提供的wsimport命令生成本地代码(wsimport为jdk的命令,之前jdk配置过环境变量,就可以直接使用)

D:\myTest>wsimport -s . http://localhost:8088/bos_crm/service/customer?wsdl
正在解析 WSDL...



正在生成代码...


正在编译代码...


D:\myTest>

3、将解析完的接口和实体类的java文件复制到项目中

4、提供spring配置文件,注册客户端代理对象

	<!-- 配置cxf客户端远程调用 -->
	<jaxws:client id="myClient" serviceClass="com.iss.crm.ICustomerService" address="http://localhost:8088/bos_crm/service/customer"/>

5、直接调用接口中的方法即可自动创建出代理对象,远程调用customer中的方法。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值