用xmlrpc调用
经过修改后的ofbiz能够支持java.sql.Date了,但是还是不支持复杂的数据结构,它没有实现SOAP中的comlexType。碰到这种情况的话,可以考虑用xmlrpc来支持。
我们在party下编一个服务,程序如下:
- package org.ofbiz.party.party;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import org.ofbiz.base.util.UtilMisc;
- import org.ofbiz.entity.GenericDelegator;
- import org.ofbiz.entity.GenericEntityException;
- import org.ofbiz.entity.GenericValue;
- import org.ofbiz.entity.condition.EntityExpr;
- import org.ofbiz.entity.condition.EntityOperator;
- import org.ofbiz.service.DispatchContext;
- import org.ofbiz.service.ServiceUtil;
- public class TestParty {
- public static Map findPersonByfirstName(DispatchContext dctx, Map context) {
- GenericDelegator delegator = dctx.getDelegator();
- String firstName = (String)context.get("firstName");
- Map result = new HashMap();
- List toBeStored = null;
- List idList = new ArrayList();
- List exprs = new ArrayList();
- // create the query expressions
- exprs.add(new EntityExpr("firstName", EntityOperator.EQUALS, firstName));
- List fList = new ArrayList();
- fList.add("paryId");
- fList.add("firstName");
- fList.add("lastName");
- try{
- toBeStored = delegator.findByAnd("Person", exprs);
- Iterator it = toBeStored.iterator();
- while(it.hasNext()){
- GenericValue p = (GenericValue)it.next();
- idList.add(UtilMisc.toMap("partyId", (String)p.get("partyId"),
- "firstName", (String)p.get("firstName"),
- "lastName", (String)p.get("lastName")));
- }
- result.put("personList", idList);
- }catch(GenericEntityException e){
- return ServiceUtil.returnError("firstName #" + firstName + "not found!");
- }
- return result;
- }
- }
service的配置文件如下:
- <service name="findPersonByfirstName" engine="java"
- location="org.ofbiz.party.party.TestParty" invoke="findPersonByfirstName" auth="false" export="true">
- <description>test</description>
- <attribute name="firstName" type="String" mode="IN" optional="false"/>
- <attribute name="personList" type="List" mode="OUT" optional="false"/>
- </service>
可以看到,这个服务的返回值是一个List,而List里面有一个Map,Map中有3个属性(partId,firstName,lastName)。
客户端程序如下:
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.xmlrpc.XmlRpcException;
- import org.apache.xmlrpc.client.XmlRpcClient;
- import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
- public class testFindPersonByFirstName {
- /**
- * @param args
- * @throws MalformedURLException
- * @throws XmlRpcException
- */
- public static void main(String[] args) throws MalformedURLException, XmlRpcException {
- // TODO Auto-generated method stub
- XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
- config.setServerURL(new URL("http://127.0.0.1:18080/webtools/control/xmlrpc"));
- XmlRpcClient client = new XmlRpcClient();
- client.setConfig(config);
- Object[] params = new Object[]{"Nie"};
- HashMap result = (HashMap) client.execute("findPersonByfirstName", params);
- Object[] oz = (Object [])result.get("personList");
- System.out.println("partyId firstName lastName");
- for(int i=0;i<oz.length;i++){
- HashMap person = (HashMap)oz[i];
- System.out.println(person.get("partyId")+" "+person.get("firstName")+" "+person.get("lastName"));
- }
- }
- }