zoukankan      html  css  js  c++  java
  • 【Web】CXF WebService 服务端和客户端 环境搭建及测试

    cxf服务端

    1.去官方下载对应的jar包:http://cxf.apache.org/

    2.maven配置相应jar包

    3.修改web.xml,完成spring和cxf配置

     1   <!-- Spring -->
     2     <context-param>
     3         <param-name>contextConfigLocation</param-name>
     4         <param-value>
     5             classpath*:/applicationContext.xml
     6         </param-value>
     7     </context-param>
     8 
     9     <listener>
    10         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    11     </listener>
    12     
    13     <!-- CXFServlet -->
    14     <servlet>
    15         <servlet-name>CXFServlet</servlet-name>
    16         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    17     </servlet>
    18     <servlet-mapping>
    19         <servlet-name>CXFServlet</servlet-name>
    20         <url-pattern>/WebService/*</url-pattern>
    21     </servlet-mapping>

    4.编写服务接口和实现

    1 @WebService(targetNamespace=Constant.NAME_SPACE)
    2 public interface FinanceService {
    3     
    4     String financeSearchById(@WebParam(name="id")Integer id);
    5     
    6 }
     1 @Transactional
     2 public class FinanceServiceImpl implements FinanceService{
     3     
     4     private static Logger logger = Logger.getLogger(FinanceServiceImpl.class);
     5     
     6     @Resource(name="financeDao")
     7     private FinanceDao financeDao;
     8 
     9     @Override
    10     public String financeSearchById(Integer id) {
    11         String json;
    12         
    13         try{
    14             json=financeDao.financeSearchById(id);
    15         }catch(Exception e){
    16             Map<String,Object> map = new HashMap<String, Object>();
    17             map.put("is_success", false);
    18             map.put("error_msg", "程序执行出错:"+e.getMessage());
    19             json = JsonUtil.objectToJson(map);
    20         }
    21         
    22         return json;
    23     }
    24 
    25 }

    5.修改spring配置文件。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
     5        xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
     6                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 
     7                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
     8     
     9     <description>Apache CXF配置</description>
    10     
    11     <import resource="classpath:META-INF/cxf/cxf.xml" />
    12     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    13     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    14     
    15     <!-- CXF Web Service Server端配置 -->
    16     <bean id="wsFinanceServiceImpl" class="com.****.******.business.webservice.server.impl.FinanceServiceImpl"/>
    17     <jaxws:server id="wsFinanceService" serviceClass="com.****.******.business.webservice.server.FinanceService" serviceBean="#wsFinanceServiceImpl"  address="/financeService" />    
    18     
    19 </beans>

    6.tomcat发布运行,http://localhost:8080/******/WebService/financeService?wsdl,看到描述文件即可

    7.编写测试类,测试。

    1 public class Test {
    2     public static void main(String[] args) {
    3         JaxWsProxyFactoryBean webService = new JaxWsProxyFactoryBean();  
    4         webService.setServiceClass(FinanceService.class);  
    5         webService.setAddress("http://localhost:8080/******/WebService/financeService?wsdl");  
    6         FinanceService financeService = (FinanceService) webService.create();  
    7         System.out.println(financeService.financeSearchById(5));  
    8     }
    9 }

    8.如预期输出结果就表示调用成功了。

    cxf客户端

    1.拿到服务端wsdl连接。

    2.使用cxf的wsdl2java生成cxf客户端代码。

       cxf下载地址:http://cxf.apache.org/download.html

     下载之后,设置添加系统变量

      变量名:CXF_HOME

      变量值:E:mysoftapache-cxf-3.0.2(cxf解压路径)

     设置完之后,cmd里面输入:

      wsdl2java -v(如果显示版本信息就表示安装成功了。)

     然后输入:

      wsdl2java  -frontend jaxws21 -p  com.****.******.business.webservice.client -d E:mysoftapache-cxf-3.0.2myclass  -client -autoNameResolution  http://localhost:8080/******/WebService/financeService?wsdl

    3.将生成的代码,除了client和service结尾的文件,和package-info.java之外的文件拷贝到工程下面,进行测试。

    4.修改applicationContext-cxf.xml文件添加下面代码:

    <!-- CXF Web Service Client端配置 -->
    <jaxws:client id="financeService" serviceClass="com.****.******.business.webservice.client.financeService" address="http://localhost:8080/******/WebService/financeService?wsdl" />

    5.编写测试类在里面添加下面代码:

    ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("/context/applicationContext-cxf.xml");  
    FinanceService bean = xmlApplicationContext.getBean(financeService.class);  
    System.out.println(bean.financeSearchById(5)); 

    6.如预期输出结果就表示调用成功了。

  • 相关阅读:
    cf------(round)#1 C. Ancient Berland Circus(几何)
    cf------(round)#1 B. Spreadsheets(模拟)
    grep 精确匹配
    解决 service iptables save 报错 please try to use systemctl
    kubernetes 亲和性调度详解
    免费好用的SSH手机客户端
    axios和drf结合的增删改查
    CDH 部署 Hadoop:5.开始安装
    OpenNebula概述
    Python 3.x 引入了函数注释
  • 原文地址:https://www.cnblogs.com/zengweiming/p/3593996.html
Copyright © 2011-2022 走看看