cxf发布和调用webservice

最近我们的系统需要和一个第三方系统对接,对接的方式是通过web service,所以就学习了一下这方面的东西

用CXF来做web service是比较简单的,本文就简单介绍一下如何一步步发布web service,以及调用现有的web service。另外如果系统已经使用了Spring MVC,那么引入CXF需要额外的步骤,见本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920。如果展现层没有用spring mvc,而是用struts2之类的,可以省去这一步

首先介绍怎么发布web service:

第1步是创建一个普通的java接口,然后用@WebService注解声明这是一个web service
Java代码   收藏代码
  1. package com.huawei.framework.webservice;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.huawei.framework.model.User;  
  6.   
  7. @WebService  
  8. public interface HelloWorld {  
  9.   
  10.     String sayHi(User user);  
  11.   
  12. }  

可以看到,这个接口非常普通,不需要继承什么额外的接口,只需要注解一个@WebService就可以

第2步当然是需要给这个接口提供一个实现类
Java代码   收藏代码
  1. package com.huawei.framework.webservice;  
  2.   
  3. import com.huawei.framework.model.User;  
  4.   
  5. public class HelloWorldImpl implements HelloWorld {  
  6.   
  7.     public String sayHi(User theUser) {  
  8.         return "Hello " + theUser.getName() + " ,your age is "  
  9.                 + theUser.getAge();  
  10.     }  
  11.   
  12. }  

这个类更加简单,连注解都省了,但是如果这个类实现了不止一个接口,那么就需要加上@WebService注解,不过一般不会这样

第3步就到了cxf出场的时候了,需要在spring配置文件中加上cxf的配置
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.                             http://cxf.apache.org/jaxws   
  8.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  9.   
  10.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  12.   
  13.     <jaxws:endpoint id="helloWorld"  
  14.         implementorClass="com.huawei.framework.webservice.HelloWorldImpl"  
  15.         address="/HelloWorld" />  
  16.   
  17. </beans>  

这里只写了CXF所需的配置,spring配置文件的其他内容已省略。用到的元素是<jaxws:endpoint>,implementorClass属性就是我们提供的实现类,然后address属性是这个web service对外暴露的路径

最后第4步是在web.xml中加载cxf
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="MyFramework" version="3.0">  
  6.       
  7.     <display-name>MyFramework</display-name>  
  8.       
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>   
  12.             WEB-INF/beans.xml,   
  13.             WEB-INF/cxf.xml  
  14.         </param-value>   
  15.     </context-param>  
  16.       
  17.     <listener>    
  18.         <listener-class>    
  19.             org.springframework.web.context.ContextLoaderListener    
  20.         </listener-class>    
  21.     </listener>    
  22.       
  23.     <servlet>  
  24.         <servlet-name>framework</servlet-name>  
  25.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  26.         <init-param>    
  27.             <param-name>contextConfigLocation</param-name>    
  28.             <param-value>WEB-INF/spring-mvc.xml</param-value>    
  29.         </init-param>    
  30.         <load-on-startup>1</load-on-startup>  
  31.     </servlet>  
  32.   
  33.     <servlet-mapping>  
  34.         <servlet-name>framework</servlet-name>  
  35.         <url-pattern>/</url-pattern>  
  36.     </servlet-mapping>  
  37.       
  38.     <servlet>    
  39.         <servlet-name>CXFServlet</servlet-name>    
  40.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
  41.         <load-on-startup>2</load-on-startup>    
  42.     </servlet>    
  43.     
  44.     <servlet-mapping>    
  45.         <servlet-name>CXFServlet</servlet-name>    
  46.         <url-pattern>/webservice/*</url-pattern>    
  47.     </servlet-mapping>   
  48.       
  49. </web-app>  

这里写的比较复杂,是为了兼容spring mvc。这里把/webservice/*作为对外暴露的路径,和上面的web service address要结合起来,比如说http://ip:port/app_name/webservice/HelloWorld,就会指向这个例子里声明的web service接口

通过以上4步,就发布了一个web service,在浏览器里输入http://ip:port/app_name/webservice,就会看到这个sayHi的web service了,并且cxf已经自动生成了wsdl文件

然后介绍一下怎么通过cxf调用已有的web service

第1步是需要“获得”web service的对应接口类,这里的“获得”有多种情况。在条件允许的情况下,比如2个子系统都是自己项目组开发的,或者对方愿意提供,那么直接拷贝过来就可以了。但一般是没有这么方便的。。那么这种情况下,就是wsdl发挥作用的时候了。只要对方发布了web service,就一定会有一个wsdl文件,那么可以根据这个wsdl文件来手写还原java接口类和所需的实体类(比如本例中的User模型类),还有一个办法就是用wsdl2java工具,生成所需的类

在这个例子中,我直接把HelloWorld.java拷贝过来就行了,注意拷贝过来的时候,@WebService注解还是需要的

第2步是配置cxf,这里用到的是<jaxws:client>元素,和上面的<jaxws:endpoint>是对应的
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.                         http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.                         http://cxf.apache.org/jaxws   
  9.                         http://cxf.apache.org/schemas/jaxws.xsd">  
  10.   
  11.     <bean id="propertyConfigurer"  
  12.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  13.         <property name="locations">  
  14.             <list>  
  15.                 <value>classpath:webservice_address.properties</value>  
  16.             </list>  
  17.         </property>  
  18.     </bean>  
  19.   
  20.     <jaxws:client id="helloClient" serviceClass="com.huawei.webservice.client.HelloWorld"  
  21.         address="${helloworld}" />  
  22.   
  23. </beans>  

其中serviceClass属性,就是指向HelloWorld所在的位置,address是目标web service的地址,也就是http://ip:port/app_name/webservice/HelloWorld

这里有一个额外的东西,就是把所有的address,放在单独的webservice_address.properties文件里管理,这样如果用到的web service比较多时,可以集中维护,不需要修改spring配置文件

第3步就是执行,十分简单
Java代码   收藏代码
  1. public class Main {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  6.                 "cxf.xml");  
  7.         HelloWorld client = (HelloWorld) context.getBean("helloClient");  
  8.   
  9.         User user = new User();  
  10.         user.setName("zhengshengdong");  
  11.   
  12.         String message = client.sayHi(user);  
  13.         System.out.println(message);  
  14.     }  
  15.   
  16. }  

上面可以看到,首先是启动Spring容器,然后像获取普通的bean一样,用http://kyfxbl.iteye.com/blog/1432952getBean()方法实例化HelloWorld接口,然后直接在其上调用sayHi()方法即可

在这个过程中,CXF对底层的复杂操作进行了封装,包括将实体数据封装成soap格式的消息,然后塞到http的request post里,发送到目标web service。然后从返回的http response中取出soap格式的消息,再反向解析,生成实体对象

http://kyfxbl.iteye.com/blog/1432952


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值