使用Axis2开发Web Service简单演示实例

首先提供其网页

http://axis.apache.org/axis2/java/core/

下载:http://labs.renren.com/apache-mirror//axis/axis2/java/core/1.5.3/axis2-1.5.3-bin.zip

然后我使用的是Eclipse开发。在Preferences-> Web Services-> Axis2Preferences设置目录信息,其他默认。

如果你手工写,还是比较麻烦的。可参考其网站文档。

开发容器 Tomcat6

Web Service在Web中使用,新建一个myServices工程。

假设该文件提供了服务入口:

/** * */ package com.service; /** * @author Administrator * */ public class FacadeService { public static String facade(String request) { String response = "resp: " ; response += request; return response; } }

假设该文件提供web services入口

/** * */ package com.services; import com.service.FacadeService; /** * @author Administrator * */ public class InvokeService { public String invoke(String req) { String response = "invoke: " ; response += FacadeService.facade(req); return response; } }

然后,使用上面的POJO发布为Web Services。如图:

然后默认即可。

发布成功后,可以在浏览器查看http://localhost:8080/myServices/services/InvokeService?wsdl

上述地址,可以通过http://localhost:8080/myServices/axis2-web/index.jsp查看服务获得。

http://localhost:8080/myServices/services/listServices

下面的是该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://services.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 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://services.com"> <wsdl:documentation>Please Type your service description here</wsdl:documentation> - <wsdl:types> - <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://services.com"> - <xs:element name="invoke"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="req" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="invokeResponse"> - <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="invokeRequest"> <wsdl:part name="parameters" element="ns:invoke" /> </wsdl:message> - <wsdl:message name="invokeResponse"> <wsdl:part name="parameters" element="ns:invokeResponse" /> </wsdl:message> - <wsdl:portType name="InvokeServicePortType"> - <wsdl:operation name="invoke"> <wsdl:input message="ns:invokeRequest" wsaw:Action="urn:invoke" /> <wsdl:output message="ns:invokeResponse" wsaw:Action="urn:invokeResponse" /> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="InvokeServiceSoap11Binding" type="ns:InvokeServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" mce_style="document" /> - <wsdl:operation name="invoke"> <soap:operation soapAction="urn:invoke" style="document" mce_style="document" /> - <wsdl:input> <soap:body use="literal" /> </wsdl:input> - <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="InvokeServiceSoap12Binding" type="ns:InvokeServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" mce_style="document" /> - <wsdl:operation name="invoke"> <soap12:operation soapAction="urn:invoke" style="document" mce_style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> - <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="InvokeServiceHttpBinding" type="ns:InvokeServicePortType"> <http:binding verb="POST" /> - <wsdl:operation name="invoke"> <http:operation location="InvokeService/invoke" /> - <wsdl:input> <mime:content type="text/xml" part="invoke" /> </wsdl:input> - <wsdl:output> <mime:content type="text/xml" part="invoke" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="InvokeService"> - <wsdl:port name="InvokeServiceHttpSoap11Endpoint" binding="ns:InvokeServiceSoap11Binding"> <soap:address location="http://localhost:8080/myServices/services/InvokeService.InvokeServiceHttpSoap11Endpoint/" /> </wsdl:port> - <wsdl:port name="InvokeServiceHttpSoap12Endpoint" binding="ns:InvokeServiceSoap12Binding"> <soap12:address location="http://localhost:8080/myServices/services/InvokeService.InvokeServiceHttpSoap12Endpoint/" /> </wsdl:port> - <wsdl:port name="InvokeServiceHttpEndpoint" binding="ns:InvokeServiceHttpBinding"> <http:address location="http://localhost:8080/myServices/services/InvokeService.InvokeServiceHttpEndpoint/" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

此时,服务器端编写完成。

其他描述可以查看该工程其他文件内容。可以不管。

客户端编写:

新建一个Java工程。

新建Web Service Client。

选择刚才的wsdl描述。进行创建。

http://localhost:8080/myServices/services/InvokeService?wsdl

Eclipse可以自动生成客户端。

上面的包含测试的情况。TestClient.jsp是该页面。

可以使用客户端代码调用了。

需要客户端调用参考,选择Junit测试。参考即可。

发送

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><invoke xmlns="http://services.com"><req>a</req></invoke></soapenv:Body></soapenv:Envelope>

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <invoke xmlns="http://services.com"> <req>a</req> </invoke> </soapenv:Body> </soapenv:Envelope>

接受

<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:invokeResponse xmlns:ns="http://services.com"><ns:return>invoke: resp: a</ns:return></ns:invokeResponse></soapenv:Body></soapenv:Envelope>

<?xml version="1.0" encoding="http://schemas.xmlsoap.org/soap/envelope/" standalone="no"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:invokeResponse xmlns:ns="http://services.com"> <ns:return>invoke: resp: a</ns:return> </ns:invokeResponse> </soapenv:Body> </soapenv:Envelope>

Java代码调用

/** * */ package com.service; import java.rmi.RemoteException; import com.services.InvokeServicePortTypeProxy; /** * @author Administrator * */ public class ServiceInvoke { /** * @param args * @throws RemoteException */ public static void main(String[] args) throws RemoteException { InvokeServicePortTypeProxy invoke = new InvokeServicePortTypeProxy(); String rsp = invoke.invoke("大家好"); System.out.println(rsp); } }

返回

invoke: resp: 大家好

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值