ofbiz的服务(service)export成web service的方法(8)

返回参数为多种数据类型的AXIS处理

假设写如下一个ofbiz的服务:
  1.      <service name="findPersonByPartyId" engine="java"
  2.             location="org.ofbiz.party.party.TestParty" invoke="findPersonByPartyId" auth="false" export="true">
  3.         <description>test</description>
  4.         <attribute name="partyId" type="String" mode="INOUT" optional="false"/>
  5.         <attribute name="firstName" type="String" mode="OUT" optional="false"/>
  6.         <attribute name="lastName" type="String" mode="OUT" optional="false"/>
  7.         <attribute name="salary" type="Double" mode="OUT" optional="false"/>
  8.     </service>

这个服务中的返回参数有string和double两种数据类型,实现的代码如下:
  1. public static Map findPersonByPartyId(DispatchContext dctx, Map context) {
  2.         GenericDelegator delegator = dctx.getDelegator();
  3.         String partyId = (String)context.get("partyId");
  4.         Map result = new HashMap();
  5.                         
  6.         try{
  7.             GenericValue person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId));
  8.             String firstName = (String)person.get("firstName");
  9.             String lastName = (String)person.get("lastName");
  10.             
  11.             result.put("partyId",partyId);
  12.             result.put("firstName", firstName);
  13.             result.put("lastName",lastName);
  14.             result.put("salary"new Double(1.12));
  15.             
  16.         }catch(GenericEntityException e){
  17.             return ServiceUtil.returnError("partyId #" + partyId + "not found!");
  18.         }                
  19.         return result;
  20.     }
编译发布后,客户端调用如下:
  1. package testClientSoap;
  2. import java.util.*;
  3. import java.net.*; 
  4. import java.rmi.*; 
  5. import javax.xml.namespace.*; 
  6. import javax.xml.rpc.*; 
  7. //import javax.wsdl.OperationType;
  8. import org.apache.axis.Message;
  9. import org.apache.axis.encoding.XMLType;
  10. import org.apache.axis.message.RPCElement;
  11. import org.apache.axis.message.RPCParam;
  12. import org.apache.axis.message.SOAPEnvelope;
  13. import org.apache.axis.client.Call; 
  14. import org.apache.axis.client.Service; 
  15. import org.apache.axis.utils.*;
  16. public class TestSOAP2 { 
  17.     
  18.     public static void main(String[] args) { 
  19.         
  20.         String message = ""
  21.         Map output;
  22.         ArrayList outputList;
  23.         String responseMessage,endpoint;
  24.     
  25.     try { 
  26.         endpoint = "http://127.0.0.1:18080/webtools/control/SOAPService/";
  27.         Call call = (Call) new Service().createCall(); 
  28.         call.setTargetEndpointAddress(new URL(endpoint));        
  29.         call.setOperationName(new QName("""findPersonByPartyId")); 
  30.                 
  31.         call.addParameter("partyId",
  32.                 org.apache.axis.Constants.XSD_STRING,
  33.                 javax.xml.rpc.ParameterMode.INOUT);
  34.         call.addParameter("firstName",
  35.                 org.apache.axis.Constants.XSD_STRING,
  36.                 javax.xml.rpc.ParameterMode.OUT);
  37.         call.addParameter("lastName",
  38.                 org.apache.axis.Constants.XSD_STRING,
  39.                 javax.xml.rpc.ParameterMode.OUT);
  40.         call.addParameter("salary",
  41.                 org.apache.axis.Constants.XSD_DOUBLE,
  42.                 javax.xml.rpc.ParameterMode.OUT);
  43.         call.setReturnType(org.apache.axis.encoding.XMLType.AXIS_VOID);
  44.         
  45.         Object responseWS = call.invoke(new Object[]{"10110"}); 
  46. //        System.out.println( "Receiving response: " +  (String) responseWS); 
  47.         output = call.getOutputParams();
  48.         String firstName = (String) output.get(new QName("""firstName"));
  49.         String lastName = (String) output.get(new QName("""lastName"));
  50.         Double salary = (Double) output.get(new QName("""salary"));
  51.         String partyId = (String) output.get(new QName("""partyId"));
  52.                 
  53.         System.out.println(partyId + ":" + firstName + " " + lastName + "'s salary is " + salary);
  54.     } catch (MalformedURLException ex) { 
  55.         message = "error: wrong url"
  56.     } catch (ServiceException ex) { 
  57.         message = "error: failed to create the call"
  58.     } catch (RemoteException ex) { 
  59.         message = "error: failed to invoke WS";
  60.     } finally { 
  61.       System.out.println(""); 
  62.       System.out.println(message); 
  63.     } 
  64.   } 
  65. }     

如此而已……

你可能感兴趣的:(ofbiz的服务(service)export成web service的方法(8))