使用Axis2开发Web服务 --- 再谈AXIOM客户端

服务端代码参照:http://blog.csdn.net/kunshan_shenbin/archive/2009/01/20/3839417.aspx

注意:测试时发现需要追加mail-1.4.jar

AXIOM客户端可先参照:http://blog.csdn.net/kunshan_shenbin/archive/2009/01/20/3839781.aspx

这次使用Axis2官方网站上的代码:

如下图所示建立工程:

使用Axis2开发Web服务 --- 再谈AXIOM客户端_第1张图片

代码:

AXIOMClient.java

package client.axiom; 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.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class AXIOMClient { private static EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/Axis2Web/services/StockQuoteService"); public static OMElement getPricePayload(String symbol) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://quickstart.samples/xsd", "tns"); OMElement method = fac.createOMElement("getPrice", omNs); OMElement value = fac.createOMElement("symbol", omNs); value.addChild(fac.createOMText(value, symbol)); method.addChild(value); return method; } public static OMElement updatePayload(String symbol, double price) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://quickstart.samples/xsd", "tns"); OMElement method = fac.createOMElement("update", omNs); OMElement value1 = fac.createOMElement("symbol", omNs); value1.addChild(fac.createOMText(value1, symbol)); method.addChild(value1); OMElement value2 = fac.createOMElement("price", omNs); value2.addChild(fac.createOMText(value2, Double.toString(price))); method.addChild(value2); return method; } public static void main(String[] args) { try { OMElement getPricePayload = getPricePayload("WSO"); OMElement updatePayload = updatePayload("WSO", 123.42); Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); sender.fireAndForget(updatePayload); System.err.println("price updated"); OMElement result = sender.sendReceive(getPricePayload); String response = result.getFirstElement().getText(); System.err.println("Current price of WSO: " + response); } catch (Exception e) { e.printStackTrace(); } } }

运行测试通过,看来上文所说的Bug现在自相矛盾了,呵呵。

你可能感兴趣的:(exception,String,测试,Class,web服务)