java用XFire开发调用webService例子

来源:互联网 发布:军用开山刀淘宝 编辑:程序博客网 时间:2024/05/29 11:26
 

XFire 概述
XFire 是 codeHaus 组织提供的一个开源框架,它构建了 POJO 和 SOA 之间的桥梁,主要特性就是支持将 POJO 通过非常简单的方式发布成 Web 服务,这种处理方式不仅充分发挥了 POJO 的作用,简化了 Java 应用转化为 Web 服务的步骤和过程,也直接降低了 SOA 的实现难度,为企业转向 SOA 架构提供了一种简单可行的方式。
XFire 目前最新的版本是 1.2.2,目前支持的特性主要包括:
支持将 Web 服务绑定到 POJO、XMLBeans、JAXB1.1、JAXB2.0 和 Castor; 
支持基于 HTTP、JMS、XMPP 等多种协议访问 Web 服务; 
支持多种 Web 服务业界重要标准如 SOAP、WSDL、Web 服务寻址(WS-Addressing)、Web 服务安全(WS-Security)等; 
支持 JSR181,可以通过 JDK5 配置 Web 服务; 
高性能的 SOAP 实现; 
服务器端、客户端代码辅助生成; 
对 Spring、Pico、Plexus 等项目的支持等。 
XFire 安装包
XFire 框架目前的最新版本是 1.2.6,可以访问 xfire.codehaus.org 下载 XFire 框架的安装包,下载时请选择“全部二进制发布包(Binary Distribution in zip package)”,而不仅仅是“XFire jar 文件(Jar of all XFire modules)”。
下载完成后,我们可以将下载的 .zip 文件解压缩到任意的文件夹中(后面的章节中使用 % XFIRE_HOME % 表示 XFire 框架的安装目录),解压缩后形成的文件目录结构如下:
api(目录) 
api 目录中是 XFire 框架中所有类(class)对应的 API 文档,为开发者使用 XFire 完成应用开发提供帮助。
examples(目录) 
examples 目录中包含了所有随 XFire 二进制包发布的实例,包括这些实例的源代码和相关 Web 应用配置内容。
lib(目录) 
lib 目录中包含 XFire 运行所需要的外部支持类包(.jar文件),可以根据不同项目所需的 XFire 特性选择所需要的支持类包。保守的方法是在 Web 项目中包含所有的外部支持类包(.jar文件)。
manual(目录) 
manual 目录中包含有 XFire 框架的帮助文档,开发者可以从这些帮助文档中学习更多运用 XFire 框架实现 SOA 的知识和技巧。
modules(目录) 
modules 目录中包含了 XFire 框架根据不同特性分别编译的二进制包文件。发布基于 XFire 框架的 Web 项目时,可以选择使用该目录下的所有 .jar 文件,也可以选择 XFire-all-1.2.6.jar 文件。
XFire-all-1.2.6.jar 
XFire 框架的二进制包文件,包含了全部的模块(modules)。
LICENSE.txt 
LICENSE.txt 文件中包含了 XFire 框架的授权协议。
NOTICE.txt 
README.txt 
这两个文件中包含了 XFire 发布时的一些有用的信息。

 如果不是用MyEclipse开发,先下载XFire的jar到lib目录。
开发
新建webservice工程myeclipse-new -project-web service project
 新建包:test.
新建接口和服务类。XFire是以接口反射机制开自动取得远程方法的。
接口:

显示代码打印1 public interface IAccount { 

2  public int account(int x,int y); 

3 }

 

实现类:


显示代码打印1 public class AccountImp implements IAccount {  

2  public int account(int x, int y) {  

3   // TODO Auto-generated method stub  

4   return x*y;  

5  }  

6 }

 

 

在webservices包下再新建 web service
service name :mywebservice
service interface: test.IAccount//手动选择导入test包下的IAccount接口
service class: test.AccountImp//业务实现类
//协议为soap协议,MyEclipse下配置保持不变。
自动生成的services.xml配置如下


显示代码打印01 <?xml version="1.0" encoding="UTF-8"?>  

02 <beans xmlns="http://xfire.codehaus.org/config/1.0">  

03  <service>  

04   <name>MyService</name>  

05   <serviceClass>test.IAccount</serviceClass>  

06   <implementationClass>test.AccountImp</implementationClass>  

07   <style>wrapped</style>  

08   <use>literal</use>  

09   <scope>application</scope>  

10  </service></beans>  

自动加入在web.xml配置如下:


显示代码打印01 <?xml version="1.0" encoding="UTF-8"?>  

02 <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

03   <servlet>  

04     <servlet-name>XFireServlet</servlet-name>  

05     <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  

06     <load-on-startup>0</load-on-startup>  

07   </servlet>  

08   <servlet-mapping>  

09     <servlet-name>XFireServlet</servlet-name>  

10     <url-pattern>/services/*</url-pattern>  

11   </servlet-mapping>  

12   <welcome-file-list>  

13     <welcome-file>index.jsp</welcome-file>  

14   </welcome-file-list>  

15 </web-app>

 

 

这两个文件都是IDE自动生成的。也可以手写。
测试一下wsdl。输入地址http://localhost:8080/WebService/services/MyService?wsdl
http://域名/工程名/services/service.xml配置的name:MyService;
结果如下:

 

显示代码打印001 <?xml version="1.0" encoding="UTF-8" ?>   

002   

003 - <wsdl:definitions targetNamespace="http://test" xmlns:tns="http://test" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">   

004   

005   

006 - <wsdl:types>   

007   

008   

009 - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://test">   

010   

011   

012 - <xsd:element name="account">   

013   

014   

015 - <xsd:complexType>   

016   

017   

018 - <xsd:sequence>   

019   

020   

021   <xsd:element maxOccurs="1" minOccurs="1" name="in0" type="xsd:int" />   

022   

023   <xsd:element maxOccurs="1" minOccurs="1" name="in1" type="xsd:int" />   

024   </xsd:sequence>   

025   </xsd:complexType>   

026   </xsd:element>   

027   

028 - <xsd:element name="accountResponse">   

029   

030   

031 - <xsd:complexType>   

032   

033   

034 - <xsd:sequence>   

035   

036   

037   <xsd:element maxOccurs="1" minOccurs="1" name="out" type="xsd:int" />   

038   </xsd:sequence>   

039   </xsd:complexType>   

040   </xsd:element>   

041   </xsd:schema>   

042   </wsdl:types>   

043   

044 - <wsdl:message name="accountResponse">   

045   

046   

047   <wsdl:part name="parameters" element="tns:accountResponse" />   

048   </wsdl:message>   

049   

050 - <wsdl:message name="accountRequest">   

051   

052   

053   <wsdl:part name="parameters" element="tns:account" />   

054   </wsdl:message>   

055   

056 - <wsdl:portType name="MyServicePortType">   

057   

058   

059 - <wsdl:operation name="account">   

060   

061   

062   <wsdl:input name="accountRequest" message="tns:accountRequest" />   

063   

064   <wsdl:output name="accountResponse" message="tns:accountResponse" />   

065   </wsdl:operation>   

066   </wsdl:portType>   

067   

068 - <wsdl:binding name="MyServiceHttpBinding" type="tns:MyServicePortType">   

069   

070   

071   <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />   

072   

073 - <wsdl:operation name="account">   

074   

075   

076   <wsdlsoap:operation soapAction="" />   

077   

078 - <wsdl:input name="accountRequest">   

079   

080   

081   <wsdlsoap:body use="literal" />   

082   </wsdl:input>   

083   

084 - <wsdl:output name="accountResponse">   

085   

086   

087   <wsdlsoap:body use="literal" />   

088   </wsdl:output>   

089   </wsdl:operation>   

090   </wsdl:binding>   

091   

092 - <wsdl:service name="MyService">   

093   

094   

095 - <wsdl:port name="MyServiceHttpPort" binding="tns:MyServiceHttpBinding">   

096   

097   

098   <wsdlsoap:address location="http://localhost:8080/WerService/services/MyService" />   

099   </wsdl:port>   

100   </wsdl:service>   

101   </wsdl:definitions>

 

 

创建webservice成功!无论用net,还flex都可以调用。 
java远程调用。
新建web工程WebServiceTest
由于在MyEclipse中只有新建webservice才会自动导入xfire的所需jar包。
所以使用时自己手动导入,把jar包考入lib文件夹下。
由于XFire的机制,先要建立跟要调用接口同样的接口名,并包含所需方法,才可以调用相应的方法。
客户调用端接口


显示代码打印1 public interface ImpAccount {  

2  public int account(int x,int y);  

3 }

 


创建调用业务实现类:(也可以是servlet或者jsp)


显示代码打印01 //导入包。  

02 import org.codehaus.xfire.XFireFactory;  

03 import org.codehaus.xfire.client.XFireProxyFactory;  

04 import org.codehaus.xfire.service.Service;  

05 import org.codehaus.xfire.service.binding.ObjectServiceFactory;  

06 public class ServiceClassTest {  

07  /**  

08   * @param args  

09   */ 

10  public static void main(String[] args) {  

11   // TODO Auto-generated method stub  

12       Service srModel = new ObjectServiceFactory()  

13             .create(IAccount.class);  

14     XFireProxyFactory factory = new XFireProxyFactory(XFireFactory  

15             .newInstance().getXFire());//创建工厂实例  

16         

17     String helloURL = "http://localhost:8080/WerService/services/MyService";  

18     try {  

19      IAccount srvc = (IAccount) factory.create(  

20                 srModel, helloURL);  

21         System.out.print("/////////"+srvc.account(2,3));  

22     } catch (MalformedURLException e) {  

23         e.printStackTrace();  

24     }  

25   }  

26     

27 }

 

 

运行java Application.
返回结果://///////6。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 陕西省医保卡丢了怎么办 小孩社保卡掉了怎么办 社区医保本丢了怎么办 宝宝医保卡掉了怎么办 同煤医疗卡丢了怎么办 杭州医保卡丢了怎么办 新的医保卡丢了怎么办 二代医保卡丢了怎么办 老医保卡丢了怎么办 上海医保卡余额用完了怎么办 身份证丢了医疗报销怎么办 取公积金身份证丢了怎么办 身份证丢了怎么办就诊卡 人在外地怎么办农村社保卡 武汉医保卡丢了怎么办 济宁社保卡丢了怎么办 农村医疗本丢了怎么办 常熟医保卡丢了怎么办 农民社保卡丢了怎么办 常熟社保卡坏了怎么办 社保卡丢失补办期看病怎么办 社保卡补办期间看病怎么办 医保卡冻结了出院结算怎么办 住院医保卡钱不够怎么办 住院押金条丢了怎么办 急用新社保卡要怎么办 看病没带社保卡怎么办 医保卡掉了住院怎么办 厦门医保卡丢了怎么办 成都医保卡丢了怎么办 长春医保卡丢了怎么办 县城医保卡丢了怎么办 医保卡丢了怎么办南宁 西安职工医保丢了怎么办 重庆医保卡丢了怎么办 外地医保卡丢了怎么办 换单位后医保卡怎么办 单位没交失业金怎么办 沈阳医保卡密码忘了怎么办 西安网约车资格证怎么办 红跑车卡丢了怎么办