webService框架cxf的简单使用

来源:互联网 发布:隔音卷帘 知乎 编辑:程序博客网 时间:2024/06/05 20:02

一、服务端提供服务

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.xsdhttp://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://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的注解。
@WebServicepublic 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中的方法。