`

Axis2创建webservice服务的方式之POJO方式

 
阅读更多

Axis2创建webservice服务的方式之POJO方式

 

Axis2创建webservice服务的方式又5种,POJO,AXIOM's OMElement,ADB,XMLBeans和

JiBX方式.

http://blog.csdn.net/larry_lv/article/details/6637570

五种方式的优缺点:

得知,POJO方式最简单,ADB方式强烈推荐.

 

下面就分别使用这两种方式创建webservice服务.

 

1. POJO方式

 

1.1 准备web工程

 

    http://jackyin5918.iteye.com/blog/1907992 中对Axis2版本中axis2.war的介绍可知,

要创建webservice先要创建一个普通的web工程,然后将axis2.war解压,用解压得到的WEB-INF

替换芯创建普通web工程的WEB-INF即可.axis2.war中的axis2-web不必使用.

 

所以要先创建一个web工程,然后替换WEB-INF.

 

这里创建web工程是:TestWebService

 

1.2 创建一个普通的java类(POJO)

package test.sevices;

public class TestService
{
  private String name ="Jack";
  
  public void setName(String newName)
  {
    this.name = newName;
  }
  
  public String getName()
  {
    return name;
  }
}

 

1.3 创建services.xml部署服务

 

根据http://jackyin5918.iteye.com/blog/1907992,部署一个服务,需要先创建一个services.xml文件.

这个文件需要放到classes文件夹下的META-INF目录中,格式一般如下:

 

<service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/">

    <description>

        Stock Quote Service

    </description>

    <messageReceivers>

        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"

                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>

        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"

                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

    </messageReceivers>

    <schema schemaNamespace="http://quickstart.samples/xsd"/>

    <parameter name="ServiceClass">samples.quickstart.service.pojo.StockQuoteService</parameter>

</service>

 

services.xml个节点含义解释: http://suselinks.us/2011/09/detailed-explanation-for-axis2-web-service-configuration-file

 

为本例创建的services.xml文件如下:

<service name="TestService" scope="application" targetNamespace="http://quickstart.samples/">
    <description>
        一个测试服务
    </description>
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
    <schema schemaNamespace="http://quickstart.samples/xsd"/>
    <parameter name="ServiceClass">test.sevices.TestService</parameter>
</service>

 

 

1.4 创建一个符合发布服务要求的目录结构

 

参考:

- StockQuoteService

   - META-INF

     - services.xml

   - lib

   - samples

     - quickstart

       - service

         - pojo

           - StockQuoteService.class

创建的目录结构为:

- TestService

   - META-INF

     - services.xml

   - lib

   - test

     - services

        - TestService.class

        

2. 将上面的TestService文件夹复制到 \WEB-INF\services\下

然后启动tomcat,开始测试:

 

(1) 查看wsdl文件:

http://localhost:8080/TestWebService/services/TestService?wsdl

得到的wsdl文件如下:

<?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://quickstart.samples/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:tns="http://quickstart.samples/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://quickstart.samples/">
  <wsdl:documentation>TestService</wsdl:documentation> 
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">
- <xs:element name="setName">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="newName" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getName">
- <xs:complexType>
  <xs:sequence /> 
  </xs:complexType>
  </xs:element>
- <xs:element name="getNameResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="getNameRequest">
  <wsdl:part name="parameters" element="ns:getName" /> 
  </wsdl:message>
- <wsdl:message name="getNameResponse">
  <wsdl:part name="parameters" element="ns:getNameResponse" /> 
  </wsdl:message>
- <wsdl:message name="setNameRequest">
  <wsdl:part name="parameters" element="ns:setName" /> 
  </wsdl:message>
- <wsdl:portType name="TestServicePortType">
- <wsdl:operation name="getName">
  <wsdl:input message="tns:getNameRequest" wsaw:Action="urn:getName" /> 
  <wsdl:output message="tns:getNameResponse" wsaw:Action="urn:getNameResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="setName">
  <wsdl:input message="tns:setNameRequest" wsaw:Action="urn:setName" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="TestServiceSoap11Binding" type="tns:TestServicePortType">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getName">
  <soap:operation soapAction="urn:getName" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="setName">
  <soap:operation soapAction="urn:setName" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="TestServiceSoap12Binding" type="tns:TestServicePortType">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getName">
  <soap12:operation soapAction="urn:getName" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="setName">
  <soap12:operation soapAction="urn:setName" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="TestServiceHttpBinding" type="tns:TestServicePortType">
  <http:binding verb="POST" /> 
- <wsdl:operation name="getName">
  <http:operation location="getName" /> 
- <wsdl:input>
  <mime:content type="application/xml" part="parameters" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="application/xml" part="parameters" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="setName">
  <http:operation location="setName" /> 
- <wsdl:input>
  <mime:content type="application/xml" part="parameters" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="TestService">
- <wsdl:port name="TestServiceHttpSoap11Endpoint" binding="tns:TestServiceSoap11Binding">
  <soap:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpSoap11Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="TestServiceHttpSoap12Endpoint" binding="tns:TestServiceSoap12Binding">
  <soap12:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpSoap12Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="TestServiceHttpEndpoint" binding="tns:TestServiceHttpBinding">
  <http:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpEndpoint/" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 

 

(2)查看schema

http://localhost:8080/TestWebService/services/TestService?xsd
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://quickstart.samples/xsd" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:tns="http://quickstart.samples/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">
- <xs:element name="setName">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="newName" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getName">
- <xs:complexType>
  <xs:sequence /> 
  </xs:complexType>
  </xs:element>
- <xs:element name="getNameResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>

 

  

(3) 调用服务查看:

 http://localhost:8080/TestWebService/services/TestService/getName

 输出:

 

- <ns:getNameResponse xmlns:ns="http://quickstart.samples/xsd">
  <ns:return>Jack</ns:return> 
  </ns:getNameResponse>

 

  

http://localhost:8080/TestWebService/services/TestService/setName?newName=Frank 设置新name(newName是setName方法的参数名)

 

再次调用 

http://localhost:8080/TestWebService/services/TestService/getName

 

输出:

- <ns:getNameResponse xmlns:ns="http://quickstart.samples/xsd">
  <ns:return>Frank</ns:return> 
  </ns:getNameResponse>

 

  参考:

http://danlley.iteye.com/blog/102163

 

附件为 工程文件,因为文件大小原因,删除了lib里面的jar包,使用的时候需要吧Axis2 1.6.2版本中的lib文件夹里的所有jar包拷贝到TestWebService\WebRoot\WEB-INF\lib

分享到:
评论

相关推荐

    axis2建webservice文档

    axis2建webservice文档 WebService大讲堂之Axis2(1):用POJO实现0配置的等

    Axis2各种方式实现WebService案例代码

    Axis2各种方式(pojo、axiom、adb、xmlbeans、jibx)实现WebService,包括客户端和服务器端

    webservice1 pojo实现0配置

    webservice1 pojo实现0配置

    用WebService传递POJO和对象数组的例子.doc

    AXIS2中自带了一个通过POJO发布为WebService 的例子。这种方式发布的WebService并不需要我们生成WSDL,而直接从Bean生成WebService(自底向上模式)。典型的问题就是,如果我们直接用AXIS2中的源代码通过ant生成arr...

    xfire教程+axis+webservice

    XFire是codeHaus组织提供的一个开源框架,它构建了POJO和SOA之间的桥梁,主要特性就是支持将POJO通过非常简单的方式发布成Web服务,这种处理方式不仅充分发挥了POJO的作用,简化了Java应用转化为Web服务的步骤和过程...

    WebService大讲堂之Axis2

    WebService大讲堂之Axis2(1):用POJO实现0配置的WebService ......................................................... 2 一、Axis2的下载和安装 ...............................................................

    Axis2 Spring整合POJO开发Web服务

    WebService从零到项目开发使用2——技术研究 源代码

    Axis2教程 包含描述目录的内容

    WebService大讲堂之Axis2系列教程 1. 用POJO实现0配置的WebService 2. 复合类型数据的传递 3. 使用services.xml文件发布WebService 4. 二进制文件传输 5. 会话(Session)管理 6. 跨服务会话(Session)管理 ...

    axis2-1.4.1及教程

    1):用POJO实现0配置的WebService 2):复合类型数据的传递 3):使用services.xml文件发布WebService 4):二进制文件传输 5):会话(Session)管理 6):跨服务会话(Session)管理 7):将Spring的装配JavaBean发布成 8...

    axis2 教程_个人备用文档

    1. 用POJO实现0配置的WebService 2. 复合类型数据的传递 3. 使用services.xml文件发布WebService 4. 二进制文件传输 5. 会话(Session)管理 6. 跨服务会话(Session)管理 7. 将Spring的装配JavaBean发布成...

    编程入门_Eclipse教程

    3.1. Axis2的简单WebService示例 10 3.1.1. 0配置POJO的Webservice 10 3.1.2. 用axis2的辅助工具发布、调用WebService 12 3.1.3. WebService会话Session的管理 24 3.1.4. 用Spring创建的JavaBean发布成WebService 29...

    AXIS2大讲堂文档

    文档1 pojo实现0配置 文档2 符合数据类型 文档3 使用services.xml发布webservice 文档4 二进制文件读取 文档5 会话管理 文档6 跨越session管理 文档7 spring的bean发布为webservice 文档8 异步调用webservice...

    pojo生成的WS服务端和自动生成的客户端

    简单易懂的axis2示例,包含了文档、开发的ant脚本。结合这eclipse插件最好了

    webservers详解

    总结网上的接口技术开发 webservice Axis配(用pojo零配置)置以及详细文档

    使用XFire+Spring构建Web+Service

    3 容易上手,可以方便快速地从pojo发布服务; 4 支持Spring、Pico、Plexus、Loom等容器; 5 灵活的Binding机制,包括默认的Aegis,xmlbeans,jaxb2,castor; 6 高性能的SOAP 栈设计; 7 支持Spring、Pico、...

Global site tag (gtag.js) - Google Analytics