设为首页 加入收藏

TOP

通向架构师的道路(第十二天)之Axis2 Web Service(三)(二)
2018-02-22 14:32:42 】 浏览:769
Tags:通向 架构 师的 道路 十二 Axis2 Web Service
<soap:request></soap:request>中间的内容,应该如下所示:

<Request xmlns="http://10.225.104.122">

    <Issuer>1234567890</Issuer>

    <Serial>11111111</Serial>

    <RevokeDate>2007-01-01</RevokeDate>

</ Response >

我们假设它是一个购买图书的定单,服务端收到这个请求后会返回一个定单信息给调用它的客户端,服务端将返回如下内容(此处不做任何业务处理,只是很简单的传值回客户端)。

<SoapResponse xmlns="http://soap.axis2.sky.org">

    <Issuer>issuer: Wrox</Issuer>

    <Serial>serial: 1111111111ISBN</Serial>

    <RevokeDate>RevocationDate: 2012-07-29</RevokeDate>

</SoapResponse>

为生成上述这个SoapResponse我们在Service端的核心代码如上面加粗部分的代码所示,由其注意这个“soapResponse.build();”。

下面我们来看这个客户端是怎么写的,我们这边用的是非阻塞式客户端

org.sky.axis2.soap.SoapServiceClient

package org.sky.axis2.soap;

 

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.client.async.AxisCallback;

import org.apache.axis2.context.MessageContext;

import javax.xml.namespace.QName;

 

public class SoapServiceClient {

         private static EndpointReference targetEPR = new EndpointReference(

                            "http://localhost:8080/Axis2Service/services/SoapService");

         public static boolean finish = false;

         public static void orderRequest() {

                   OMFactory factory = OMAbstractFactory.getOMFactory();

                   OMNamespace omNs = factory.createOMNamespace(

                                     "http://soap.axis2.sky.org", "");

                   OMElement issuer = factory.createOMElement("Issuer", omNs);

                   OMElement serial = factory.createOMElement("Serial", omNs);

                   OMElement revocationDate = factory.createOMElement("RevocationDate",

                                     omNs);

                   issuer.setText("Wrox");

                   serial.setText("1111111111ISBN");

                   revocationDate.setText("2012-07-29");

                   OMElement requestSoapMessage = factory.createOMElement("request", omNs);

                   requestSoapMessage.addChild(issuer);

                   requestSoapMessage.addChild(serial);

                   requestSoapMessage.addChild(revocationDate);

                   requestSoapMessage.build();

                   Options options = new Options();

                   options.setTo(targetEPR);

                   ServiceClient sender = null;

                   try {

                            AxisCallback callback = new AxisCallback() {

                                     public void onMessage(MessageContext msgContext) {

                                               OMElement result = msgContext.getEnvelope().getBody()

                                                                 .getFirstElement();

                                               // System.out.println(msgContext.toString());

                                               // System.out.println(msgContext.getEnvelope().toString());

                                               System.out.println(msgContext.getEnvelope().getBody()

                                                                 .getFirstElement());

                                               finish = true;

                                     }

                                     public void onFault(MessageContext msgContext) {

                                               System.out.println(msgContext.getEnvelope().getBody()

                                                                 .getFault().toString());

                                     }

                                     public void onError(Exception e) {

                                     }

                                     public void onComplete() {

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java 开发 .gitignore 文件包含 ... 下一篇Java 非阻塞 IO 和异步 IO

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目