加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 00:13:54 所属栏目:安全 来源:网络整理
导读:Axis2创建webservice服务的方式之POJO方式 ? Axis2创建webservice服务的方式又5种,POJO,AXIOM's OMElement,ADB,XMLBeans和 JiBX方式. 由 http://www.voidcn.com/article/p-gvwzcvqy-wp.html 五种方式的优缺点: 得知,POJO方式最简单,ADB方式强烈推荐. ? 下面

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

?

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

JiBX方式.

http://www.voidcn.com/article/p-gvwzcvqy-wp.html

五种方式的优缺点:

得知,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-INFservices下

然后启动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包拷贝到TestWebServiceWebRootWEB-INFlib

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读