用axis调用服务
通过修改昨天说的ofbiz的源代码后,createPerson终于能够访问到WSDL的文件了,下一步就是编写客户端来调用了。
我们使用axis1.4来编写客户端,代码如下:
- package testClientSoap;
- import java.util.*;
- import java.net.*;
- import java.rmi.*;
- import javax.xml.namespace.*;
- import javax.xml.rpc.*;
- //import javax.wsdl.OperationType;
- import org.apache.axis.Message;
- import org.apache.axis.encoding.XMLType;
- import org.apache.axis.message.RPCElement;
- import org.apache.axis.message.RPCParam;
- import org.apache.axis.message.SOAPEnvelope;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.utils.*;
- public class TestCreatePerson {
- private static Map getResponseParams(Message respMessage) {
- Map mRet = new Hashtable();
- try {
- SOAPEnvelope resEnv = respMessage.getSOAPEnvelope();
- List bodies = resEnv.getBodyElements();
- Iterator i = bodies.iterator();
- while (i.hasNext()) {
- Object o = i.next();
- if (o instanceof RPCElement) {
- RPCElement body = (RPCElement) o;
- List params = null;
- params = body.getParams();
- Iterator p = params.iterator();
- while (p.hasNext()) {
- RPCParam param = (RPCParam) p.next();
- mRet.put(param.getName(), param.getValue());
- System.out.println("SOAP Client Param - " + param.getName() + "=" + param.getValue());
- }
- }
- }
- } catch (org.apache.axis.AxisFault e) {
- System.out.println("AxisFault");
- } catch (org.xml.sax.SAXException e) {
- System.out.println("SAXException");
- }
- return mRet;
- }
- public static void main(String[] args) {
- String message = "";
- Map output;
- ArrayList outputList;
- String responseMessage,firstName,lastName,endpoint;
- try {
- endpoint = "http://127.0.0.1:1234/webtools/control/SOAPService/";
- Call call = (Call) new Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName(new QName("createPerson", "createPerson"));
- call.addParameter("firstName",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
- call.addParameter("middleName",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
- call.addParameter("lastName",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
- call.addParameter("comments",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
- call.setReturnType(org.apache.axis.Constants.XSD_STRING);
- Object responseWS = call.invoke(new Object[]{"admin", "ofbiz", "John", "A", "Tower", "WOWOWO"});
- System.out.println( "Receiving response: " + (String) responseWS);
- output = call.getOutputParams();
- getResponseParams(call.getMessageContext().getResponseMessage());
- } catch (MalformedURLException ex) {
- message = "error: wrong url";
- } catch (ServiceException ex) {
- message = "error: failed to create the call";
- } catch (RemoteException ex) {
- message = "error: failed to invoke WS";
- } finally {
- System.out.println("");
- System.out.println(message);
- }
- }
- }
编译运行这个代码,可以很顺利的调用成功。不要高兴的太早,注意到了吗?createPerson的service的auth属性,是false的,意思是说,调用createPerson这个类,是不需要通过ofbiz认证的,
但是我们看一下service.xml文件,很多的service是需要认证的,那么我们把createPerson的auth改成true,重启一下ofbiz,用浏览器访问wsdl文件,没有问题,再运行上面的程序,程序抛出RemoteException这个异常
通过看服务端的日志,的确是没有认证导致的。
怎么解决呢?很简单,我们把input参数增加两个
- call.addParameter("login.username",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
- call.addParameter("login.password",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
login.username和login.password这两个参数加到input里面,再编译一下上面的客户端程序,呵呵,成功了!
我们再测试一下,把createPerson的auth改回false,重启一下ofbiz,再运行一下上面的客户端程序,依然能成功。
那么我们基本上可以得出一个结论,只要把login.username和login.password加到输入参数里面,无论auth为true或者false,都能通吃。