一、总结前一天
前一天中我们报告了如何生成一个Axis2的WebService, 如何布署以及4种差异的客户端, 它们是: 传统式, 非阻塞式, 双工模式, 双工非阻塞。
而且我们看到了一个Axis2的Web Service的布署描写:
<service name="HelloWorld">
<parameter name="ServiceClass">org.sky.axis2.helloworld.HelloWorld</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
<actionMapping>urn:sayHello</actionMapping>
</operation>
</service>
这个描写代表我们的这个Web Service的要领有一进一出两个参数,且是Axis2特有的” OMElement”范例。
那么,我们想要一个public String sayHello(String name)这样的一种简朴的java范例来书写我们的WebService可以吗?
虽然,只不外我们的布署描写和我们的客户端挪用返回值上稍稍有一些纷歧样。
二、利用简朴Java范例书写我们的WebService
昆山软件开拓 述的SOAP语句" src="/uploads/allimg/c180208/151P34105ZQ0-1S24.jpg" />
HelloJava类:
package org.sky.axis2.helloworld.javatype;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMElement;
public class HelloJava {
public String sayHello(String name) throws XMLStreamException {
StringBuffer hello = new StringBuffer();
hello.append("hello: ");
hello.append(name);
return hello.toString();
}
}
Service描写文件:
此时我们相应的布署文件就是service.xml文件内容稍稍纷歧样,来看
<service name="HelloJava">
<parameter name="ServiceClass" locked="false">
org.sky.axis2.helloworld.javatype.HelloJava
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<actionMapping>urn:sayHello</actionMapping>
</service>
我们把这个WebService布署入Tomcat后启动起来看
昆山软件开拓 述的SOAP语句" src="/uploads/allimg/c180208/151P3410611520-2KP.jpg" />
是不是多了一个Service叫”HelloJava”。
我们把这个Service的wsdl地点导入SOAP UI东西并生成模仿客户端,昆山软件开发,然后再来看看它的挪用与返回值。
昆山软件开拓 述的SOAP语句" src="/uploads/allimg/c180208/151P34106443F-3ZM.jpg" />
昆山软件开拓 述的SOAP语句" src="/uploads/allimg/c180208/151P3410AX60-455O.jpg" />
留意这个返回值:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:sayHelloResponse xmlns:ns="http://javatype.helloworld.axis2.sky.org">
<ns:return>hello: Simon Shen</ns:return>
</ns:sayHelloResponse>
</soapenv:Body>
</soapenv:Envelope>
标有赤色加粗的部门,横竖一粗,就有用就是好对象(我喜欢粗,YEAH)。
再来较量一下昨天我们用“org.apache.axis2.receivers.RawXMLINOutMessageReceiver”这个Service描写生成的返回值
org.apache.axis2.receivers.RawXMLINOutMessageReceiver
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<hel:sayHello xmlns:hel="http://helloworld.axis2.sky.org">
<!--Optional:-->
<hel:sayHello>Monica</hel:sayHello>
</hel:sayHello>
</soapenv:Body>
</soapenv:Envelope>
看到区别没有?于是,昆山软件公司,变动我们的客户端挪用代码如下,在此我们利用异步双工模式:
org.sky.axis2.helloworld.javatype.HelloJavaWithReturnDualNonBlock
package org.sky.axis2.helloworld.javatype;
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.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
public class HelloJavaWithReturnDualNonBlock {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/Axis2Service/services/HelloJava");
public static boolean finish = false;
public void sayHello() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://javatype.helloworld.axis2.sky.org", "");
OMElement method = fac.createOMElement("sayHello", omNs);
OMElement name = fac.createOMElement("name", omNs);
name.setText("ymk");
method.addChild(name);
method.build();
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setUseSeparateListener(true);
options.setAction("urn:sayHello");
ServiceClient sender = null;
HelloJavaNonBlockCB callback = new HelloJavaNonBlockCB();
try {
sender = new ServiceClient();
sender.engageModule(Constants.MODULE_ADDRESSING);
sender.setOptions(options);
sender.sendReceiveNonBlocking(method, callback);
synchronized (callback) {
try {
callback.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
sender.cleanup();
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
HelloJavaWithReturnDualNonBlock testClient = new HelloJavaWithReturnDualNonBlock();
testClient.sayHello();
}
}