返回参数为多种数据类型的AXIS处理
假设写如下一个ofbiz的服务:
- <service name="findPersonByPartyId" engine="java"
- location="org.ofbiz.party.party.TestParty" invoke="findPersonByPartyId" auth="false" export="true">
- <description>test</description>
- <attribute name="partyId" type="String" mode="INOUT" optional="false"/>
- <attribute name="firstName" type="String" mode="OUT" optional="false"/>
- <attribute name="lastName" type="String" mode="OUT" optional="false"/>
- <attribute name="salary" type="Double" mode="OUT" optional="false"/>
- </service>
这个服务中的返回参数有string和double两种数据类型,实现的代码如下:
- public static Map findPersonByPartyId(DispatchContext dctx, Map context) {
- GenericDelegator delegator = dctx.getDelegator();
- String partyId = (String)context.get("partyId");
- Map result = new HashMap();
- try{
- GenericValue person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId));
- String firstName = (String)person.get("firstName");
- String lastName = (String)person.get("lastName");
- result.put("partyId",partyId);
- result.put("firstName", firstName);
- result.put("lastName",lastName);
- result.put("salary", new Double(1.12));
- }catch(GenericEntityException e){
- return ServiceUtil.returnError("partyId #" + partyId + "not found!");
- }
- return result;
- }
- 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 TestSOAP2 {
- public static void main(String[] args) {
- String message = "";
- Map output;
- ArrayList outputList;
- String responseMessage,endpoint;
- try {
- endpoint = "http://127.0.0.1:18080/webtools/control/SOAPService/";
- Call call = (Call) new Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName(new QName("", "findPersonByPartyId"));
- call.addParameter("partyId",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.INOUT);
- call.addParameter("firstName",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.OUT);
- call.addParameter("lastName",
- org.apache.axis.Constants.XSD_STRING,
- javax.xml.rpc.ParameterMode.OUT);
- call.addParameter("salary",
- org.apache.axis.Constants.XSD_DOUBLE,
- javax.xml.rpc.ParameterMode.OUT);
- call.setReturnType(org.apache.axis.encoding.XMLType.AXIS_VOID);
- Object responseWS = call.invoke(new Object[]{"10110"});
- // System.out.println( "Receiving response: " + (String) responseWS);
- output = call.getOutputParams();
- String firstName = (String) output.get(new QName("", "firstName"));
- String lastName = (String) output.get(new QName("", "lastName"));
- Double salary = (Double) output.get(new QName("", "salary"));
- String partyId = (String) output.get(new QName("", "partyId"));
- System.out.println(partyId + ":" + firstName + " " + lastName + "'s salary is " + salary);
- } 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);
- }
- }
- }
如此而已……