当前位置: 移动技术网 > IT编程>开发语言>Java > Java编程中使用XFire框架调用WebService程序接口

Java编程中使用XFire框架调用WebService程序接口

2019年07月22日  | 移动技术网IT编程  | 我要评论

 java调用webservice,当你刚开始接触的时候你会觉得它是一个恶梦,特别是没有一个统一的标准实现,比起.net的那些几步就可以完成的webservice实现,我们看着java的实现真是伤心啊。但就算是伤心,我们也还是要完成的。java也不乏比较好的实现,如xfire,jersey,cxf。 这里我们就一起来看一下xfire的实现。

 1)首先,当然是要下包啦,这个普通人都知道。http://xfire.codehaus.org/download可以到这里去下,可以下all也可以下distribution。但建议还是下all的,免得一堆奇怪的问题搞得你一点信心都没了。

 包弄下来了那么怎么办呢?放进项目里啊。貌似废话,但很多人就是不知道下下来要干什么用。

 建一个新项目,比较我的是xfirewebservice,这里当然是建web项目啦。

20151228150314590.png (249×508)20151228150342722.png (244×552)

我这里是把它所有的包都放到这里面了,毕竟我们写例子,就没必要挑三拣四了,随便点吧,如果想看看异常信息的朋友可以不把全部放进去,慢慢地加入,以后遇到错误也好排除,但我们这里就不那么做了,毕竟一般缺少什么类那些的异常没什么难看的,大家可以自己排除。

 2)我们首先来了解一下xfire与其他webservice框架的不同,它最大的不同之处在于它需要一个接口,而且如果需要用xfire来调用相应的webservice必须知道接口的定义,感觉这里有点限制了。但除了这点,xfire调用webservice,那是相当的方便,就跟调用本地方法一样。我们直接来看例子:

 首先是最重要的接口:

public interface ireaderservice { 
  public reader getreader(string name,string password); 
  public list<reader> getreaders(); 
} 
 有接口,当然也要有实现类,不然接口就没什么意义了。
public class readerservice implements ireaderservice{ 
  public reader getreader(string name,string password) { 
    return new reader(name,password); 
  } 
   
  public list<reader> getreaders(){ 
    list<reader> readerlist = new arraylist<reader>(); 
    readerlist.add(new reader("shun1","123")); 
    readerlist.add(new reader("shun2","123")); 
    return readerlist; 
  } 
} 

  也看一下javabean,reader类:

public class reader{ 
  private static final long serialversionuid = 1l; 
  private string name; 
  private string password; 
   
  public reader(){} 
  public reader(string name,string password) { 
    this.name = name; 
    this.password = password; 
  } 
    //get/set方法省略 
  public string tostring(){ 
    return "name:"+name+",password:"+password; 
  } 
   
} 

  注意,我们这里的reader类实现了serializable接口,为什么呢?这里,首先我们需要了解webservice的原理,对于java来讲,如果我们需要在互联网上传对象,很多人当然会想到序列化,对了,这里就是序列化,因为我们需要把reader作为参数来传递。这在以前的版本中是需要强制实现,否则会报错,但现在的最新的版本(其实最新的也是07年的,因为xfire已经停止开发,被apache合并为cxf项目,这个我们之后再讲)已经不需要了,至于是用什么方式实现的,我们这里暂时不深究,因为它已经被合并到cxf中,我们如果要深入学习,应该学习cxf较好。

 3)当我们完成上面的接口和javabean的编写后,很多人会问,我看很多webservice都会有wsdl文件,那你这个怎么来的?在讲这个之前,我们来讨论一下什么是wsdl。也许很多公司提供的接口都还是只是一个http地址,返回xml这样的格式,我们的也是。这有一个好处,也有一个坏处。好处是我们开发的难度小了,而坏处是我们需要提供给用户一堆说明文件,每个返回的xml标签是什么意思,这倒也没啥,但就是比较烦而已。而webservice呢,坏处就是我们开发的东西稍微多了点,而好处是我们不用再写那么多说明文件,因为有一个统一的说明,叫wsdl,这个是webservice的说明文档,是统一的,无论什么语言都一样,所以不存在谁看不懂的问题。

 而这里,当我们部署完成xfire后,它就可以帮我们生成wsdl文件。

 问题是怎么部署,这个其实也简单。我们在src目录下新建一个文件夹meta-inf,再建它的一个字文件夹xfire,里面建立文件services.xml。之后的结构如下:
 

 有人会问为什么要建到src目录下,其实不是规定建到这里的,但因为我们需要让开发工具帮我们自己部署这几个文件,所以我们放到这里,eclipse就可以帮我们自己部署到tomcat或者其他的容器中。注意,这个文件所在文件夹层次是固定的,不可以修改。

 我们直接看一下servics.xml:

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://xfire.codehaus.org/config/1.0"> 
  <service> 
    <!-- webserviceq名称,调用时需要指定这个 --> 
    <name>readerservice</name> 
    <!-- 这个一般是自己公司的网址,意义不大 --> 
    <namespace>http://test/helloservice</namespace> 
    <!-- 接口类 --> 
    <serviceclass>com.xfire.servlet.ireaderservice</serviceclass> 
    <!-- 实现类 --> 
    <implementationclass>com.xfire.servlet.readerservice</implementationclass> 
  </service> 
</beans> 


 看着注释一般都没问题的。

 4)很多人以为这样就行了,不,还没行,你指定了这个,那别人怎么访问呢。怎么把相应的请求转发到xfire那里,让它进行处理呢。这里又需要修改web.xml了。
 修改后如下:

<?xml version="1.0" encoding="utf-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
  id="webapp_id" version="3.0"> 
  <servlet> 
    <servlet-name>xfireservlet</servlet-name> 
    <servlet-class>org.codehaus.xfire.transport.http.xfireconfigurableservlet</servlet-class> 
  </servlet> 
 
  <servlet-mapping> 
    <servlet-name>xfireservlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
  </servlet-mapping> 
</web-app> 

  其实也就是添加了一个servlet和对应的mapping。接下来,我们在浏览器上直接输入:

http://localhost:8080/xfirewebservice/services/readerservice?wsdl

 我们可以看到:

20151228150440836.png (243×121)

这里显示的就是wsdl,它会显示我们定义的方法,返回的类型。后面有对wsdl的讲解。
5)上面四步完成后,我们就完成了webservice的部署了。别人就可以调用相应的webservice来访问我们的方法了。下面我们就用xfire提供的client来访问一下我们刚才发布的webservice:

public class readerclient { 
  public static void main(string[] args) { 
    //这里是创建一个service,需要传入一个接口类,因为我们后面必须调用相应的接口方法 
    service srcmodel = new objectservicefactory().create(ireaderservice.class); 
    //代理工厂,这里是为了后面创建相应的接口类 
    xfireproxyfactory factory = new xfireproxyfactory(xfirefactory.newinstance().getxfire()); 
    //webservice地址,不需要加wsdl 
    string readerserviceurl = "http://localhost:8080/xfirewebservice/services/readerservice"; 
     
    try { 
      //利用工厂返回相应的接口类 
      ireaderservice readerservice = (ireaderservice)factory.create(srcmodel,readerserviceurl); 
 
      reader reader = readerservice.getreader("shun","123"); 
      system.out.println(reader); 
    } catch (malformedurlexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

 这样,我们看到输出结果为:

20151228150517885.png (1200×624)

wsdl文件结构分析


wsdl (web services description language,web服务描述语言)是一种xml application,他将web服务描述定义为一组服务访问点,客户端可以通过这些服务访问点对包含面向文档信息或面向过程调用的服务进行访问(类似远程过程调用)。wsdl首先对访问的操作和访问时使用的请求/响应消息进行抽象描述,然后将其绑定到具体的传输协议和消息格式上以最终定义具体部署的服务访问点。相关的具体部署的服务访问点通过组合就成为抽象的web服务。 本文将详细讲解wsdl文档的结构,并分析每个元素的作用。

一:wsdl定义

    wsdl是一个用于精确描述web服务的文档,wsdl文档是一个遵循wsdl xml模式的xml文档。wsdl 文档将web服务定义为服务访问点或端口的集合。在 wsdl 中,由于服务访问点和消息的抽象定义已从具体的服务部署或数据格式绑定中分离出来,因此可以对抽象定义进行再次使用:消息,指对交换数据的抽象描述;而端口类型,指操作的抽象集合。用于特定端口类型的具体协议和数据格式规范构成了可以再次使用的绑定。将web访问地址与可再次使用的绑定相关联,可以定义一个端口,而端口的集合则定义为服务。

   一个wsdl文档通常包含7个重要的元素,即types、import、message、porttype、operation、binding、 service元素。这些元素嵌套在definitions元素中,definitions是wsdl文档的根元素。文章的下一部分将会详细介绍wsdl 的基本结构。

二:wsdl的基本结构--概述

如第一部分最后描述的那样,一个基本的wsdl文档包含7个重要的元素。下面将分别介绍这几个元素以及他们的作用。

wsdl 文档在web服务的定义中使用下列元素:

·                types - 数据类型定义的容器,它使用某种类型系统(一般地使用xml schema中的类型系统)。

·                message - 通信消息的数据结构的抽象类型化定义。使用types所定义的类型来定义整个消息的数据结构。

·                operation - 对服务中所支持的操作的抽象描述,一般单个operation描述了一个访问入口的请求/响应消息对。

·                porttype - 对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。

·                binding - 特定端口类型的具体协议和数据格式规范的绑定。

·                port - 定义为协议/数据格式绑定与具体web访问地址组合的单个服务访问点。

·                service- 相关服务访问点的集合。

wsdl的xml schema可以参照如下网址:http://schemas.xmlsoap.org/wsdl/

三:wsdl的基本结构--详述

本节将通过一个例子详细描述wsdl文档每个元素的作用。下面一个例子是一个简单的wsdl文档的内容,该文档的产生可以参见我的另外一篇文章:xfire开发实例--helloworld篇 。

一个简单的web service的wsdl文档,该服务支持名为sayhello的唯一操作,该操作通过在http上运行soap协议来实现的。该请求接受一个字符串name,经过处理后返回一个简单的字符串。文档如下:

<?xml version="1.0" encoding="utf-8" ?> 
<wsdl:definitions 
  targetnamespace="http://com.liuxiang.xfiredemo/helloservice" 
  xmlns:tns="http://com.liuxiang.xfiredemo/helloservice" 
  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/"> 
  <wsdl:types> 
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema" 
      attributeformdefault="qualified" elementformdefault="qualified" 
      targetnamespace="http://com.liuxiang.xfiredemo/helloservice"> 
      <xsd:element name="sayhello"> 
        <xsd:complextype> 
          <xsd:sequence> 
            <xsd:element maxoccurs="1" minoccurs="1" 
              name="name" nillable="true" type="xsd:string" /> 
          </xsd:sequence> 
        </xsd:complextype> 
      </xsd:element> 
      <xsd:element name="sayhelloresponse"> 
        <xsd:complextype> 
          <xsd:sequence> 
            <xsd:element maxoccurs="1" minoccurs="1" 
              name="out" nillable="true" type="xsd:string" /> 
          </xsd:sequence> 
        </xsd:complextype> 
      </xsd:element> 
    </xsd:schema> 
  </wsdl:types> 
  <wsdl:message name="sayhelloresponse"> 
    <wsdl:part name="parameters" element="tns:sayhelloresponse" /> 
  </wsdl:message> 
  <wsdl:message name="sayhellorequest"> 
    <wsdl:part name="parameters" element="tns:sayhello" /> 
  </wsdl:message> 
  <wsdl:porttype name="helloserviceporttype"> 
    <wsdl:operation name="sayhello"> 
      <wsdl:input name="sayhellorequest" 
        message="tns:sayhellorequest" /> 
      <wsdl:output name="sayhelloresponse" 
        message="tns:sayhelloresponse" /> 
    </wsdl:operation> 
  </wsdl:porttype> 
  <wsdl:binding name="helloservicehttpbinding" 
    type="tns:helloserviceporttype"> 
    <wsdlsoap:binding style="document" 
      transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="sayhello"> 
      <wsdlsoap:operation soapaction="" /> 
      <wsdl:input name="sayhellorequest"> 
        <wsdlsoap:body use="literal" /> 
      </wsdl:input> 
      <wsdl:output name="sayhelloresponse"> 
        <wsdlsoap:body use="literal" /> 
      </wsdl:output> 
    </wsdl:operation> 
  </wsdl:binding> 
  <wsdl:service name="helloservice"> 
    <wsdl:port name="helloservicehttpport" 
      binding="tns:helloservicehttpbinding"> 
      <wsdlsoap:address 
        location="http://localhost:8080/xfire/services/helloservice" /> 
    </wsdl:port> 
  </wsdl:service> 
</wsdl:definitions> 

types元素使用xml模式语言声明在wsdl文档中的其他位置使用的复杂数据类型与元素;

import元素类似于xml模式文档中的import元素,用于从其他wsdl文档中导入wsdl定义;
message元素使用在wsdl文档的type元素中定义或在import元素引用的外部wsdl文档中定义的xml模式的内置类型、复杂类型或元素描述了消息的有效负载;
porttype元素和operation元素描述了web服务的接口并定义了他的方法。porttype元素和operation元素类似于java接口中定义的方法声明。operation元素使用一个或者多个message类型来定义他的输入和输出的有效负载;
binding元素将porttype元素和operation元素赋给一个特殊的协议和编码样式;
service元素负责将internet地址赋给一个具体的绑定;

1、definitions元素

所有的wsdl文档的根元素均是definitions元素。该元素封装了整个文档,同时通过其name提供了一个wsdl文档。除了提供一个命名空间外,该元素没有其他作用,故不作详细描述。

下面的代码是一个definitions元素的结构:

<wsdl:definitions 
  targetnamespace="http://com.liuxiang.xfiredemo/helloservice" 
  xmlns:tns="http://com.liuxiang.xfiredemo/helloservice" 
  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/"> 
</wsdl:definitions> 

2、types元素

wsdl采用了w3c xml模式内置类型作为其基本类型系统。types元素用作一个容器,用于定义xml模式内置类型中没有描述的各种数据类型。当声明消息部分的有效负载时,消息定义使用了在types元素中定义的数据类型和元素。在本文的wsdl文档中的types定义:

<wsdl:types> 
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema" 
      attributeformdefault="qualified" elementformdefault="qualified" 
      targetnamespace="http://com.liuxiang.xfiredemo/helloservice"> 
      <xsd:element name="sayhello"> 
        <xsd:complextype> 
          <xsd:sequence> 
            <xsd:element maxoccurs="1" minoccurs="1" 
              name="name" nillable="true" type="xsd:string" /> 
          </xsd:sequence> 
        </xsd:complextype> 
      </xsd:element> 
      <xsd:element name="sayhelloresponse"> 
        <xsd:complextype> 
          <xsd:sequence> 
            <xsd:element maxoccurs="1" minoccurs="1" 
              name="out" nillable="true" type="xsd:string" /> 
          </xsd:sequence> 
        </xsd:complextype> 
      </xsd:element> 
    </xsd:schema> 
  </wsdl:types> 

上面是数据定义部分,该部分定义了两个元素,一个是sayhello,一个是sayhelloresponse:

sayhello:定义了一个复杂类型,仅仅包含一个简单的字符串,将来用来描述操作的参入传入部分;

sayhelloresponse:定义了一个复杂类型,仅仅包含一个简单的字符串,将来用来描述操作的返回值;

3、import元素

import元素使得可以在当前的wsdl文档中使用其他wsdl文档中指定的命名空间中的定义元素。本例子中没有使用import元素。通常在用户希望模块化wsdl文档的时候,该功能是非常有效果的。

import的格式如下:

<wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/> 

必须有namespace属性和location属性:

namespace属性:值必须与正导入的wsdl文档中声明的targetnamespace相匹配;

location属性:必须指向一个实际的wsdl文档,并且该文档不能为空。

4、message元素

message元素描述了web服务使用消息的有效负载。message元素可以描述输出或者接受消息的有效负载;还可以描述soap文件头和错误detail元素的内容。定义message元素的方式取决于使用rpc样式还是文档样式的消息传递。在本文中的message元素的定义,本文档使用了采用文档样式的消息传递:

<wsdl:message name="sayhelloresponse"> 
    <wsdl:part name="parameters" element="tns:sayhelloresponse" /> 
  </wsdl:message> 
  <wsdl:message name="sayhellorequest"> 
    <wsdl:part name="parameters" element="tns:sayhello" /> 
  </wsdl:message> 

该部分是消息格式的抽象定义:定义了两个消息sayhelloresponse和sayhellorequest:

sayhellorequest:sayhello操作的请求消息格式,由一个消息片断组成,名字为parameters,元素是我们前面定义的types中的元素;

sayhelloresponse:sayhello操作的响应消息格式,由一个消息片断组成,名字为parameters,元素是我们前面定义的types中的元素;

如果采用rpc样式的消息传递,只需要将文档中的element元素应以修改为type即可。

5、porttype元素

porttype元素定义了web服务的抽象接口。该接口有点类似java的接口,都是定义了一个抽象类型和方法,没有定义实现。在wsdl中, porttype元素是由binding和service元素来实现的,这两个元素用来说明web服务实现使用的internet协议、编码方案以及 internet地址。

一个porttype中可以定义多个operation,一个operation可以看作是一个方法,本文中wsdl文档的定义:

  <wsdl:porttype name="helloserviceporttype"> 
    <wsdl:operation name="sayhello"> 
      <wsdl:input name="sayhellorequest" 
        message="tns:sayhellorequest" /> 
      <wsdl:output name="sayhelloresponse" 
        message="tns:sayhelloresponse" /> 
    </wsdl:operation> 
  </wsdl:porttype> 

porttype定义了服务的调用模式的类型,这里包含一个操作sayhello方法,同时包含input和output表明该操作是一个请求/响应模式,请求消息是前面定义的sayhellorequest,响应消息是前面定义的sayhelloresponse。input表示传递到web服务的有效负载,output消息表示传递给客户的有效负载。

6、binding

binding元素将一个抽象porttype映射到一组具体协议(soao和http)、消息传递样式、编码样式。通常binding元素与协议专有的元素和在一起使用,本文中的例子:

  <wsdl:binding name="helloservicehttpbinding" 
    type="tns:helloserviceporttype"> 
    <wsdlsoap:binding style="document" 
      transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="sayhello"> 
      <wsdlsoap:operation soapaction="" /> 
      <wsdl:input name="sayhellorequest"> 
        <wsdlsoap:body use="literal" /> 
      </wsdl:input> 
      <wsdl:output name="sayhelloresponse"> 
        <wsdlsoap:body use="literal" /> 
      </wsdl:output> 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网