[英]Is it better to return a java.util.List or a java.util.Collection from my Restful Web Service?
[英]java.util.collection return from webservice
我正在調用將java.util.Collection
輸出參數還給我的服務。 現在我的問題是如何從該對象檢索數據? 如果使用Java打印,則會得到類似[[Ljava.lang.Object;@7ff4d7c0]
。 這很正常,因為我只是在打印對象。
該服務來自ofbiz項目,並且運行正常。 當我使用ofbiz的Web服務測試器時,返回值為:
{{party=[GenericEntity:Party][partyId,10045(java.lang.String)][partyTypeId,PERSON(java.lang.String)]}, {party=[GenericEntity:Party][partyId,10119(java.lang.String)][partyTypeId,PERSON(java.lang.String)]}}`
如何用Java實現呢?
這是我的代碼:
public static void main(String[] args) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8080/webtools/control/xmlrpc"));
config.setEnabledForExceptions(true);
config.setEnabledForExtensions(true);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Map paramMap = new HashMap();
Map map1 = new HashMap();
//Verplichte parameters
paramMap.put("login.username", "admin");
paramMap.put("login.password", "opentaps");
paramMap.put("userLoginId", "David");
Object[] params = new Object[]{paramMap};
Map result = (Map) client.execute("getPartyFromUserLogin", params);
//System.out.println(result.values());
Collection parties = null;
parties = (Collection) result.values();
System.out.println("Got parties: " + parties.size());
System.out.println(parties.toString());
Iterator it = parties.iterator();
while(it.hasNext()){
Party object = (Party)it.next();
System.out.println("ok : "+ object.getPartyId());
}
}
好的,現在我正在嘗試對其進行迭代並將其轉換為Party對象( import org.opentaps.base.entities.Party;
)。 猜猜它只是在錯誤的班級上鑄造?
錯誤日志:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.opentaps.base.entities.Party
at test.xmlrpcGetPartyEmail.main(xmlrpcGetPartyEmail.java:56)
使用此代碼后的錯誤日志:
Iterator it = parties.iterator();
while(it.hasNext()){
System.out.println("ok : "+ it.next().getClass());
}
class java.util.HashMap$Values
Got parties: 1
[[Ljava.lang.Object;@2380bfe1]
ok : class [Ljava.lang.Object;
服務器日志:
ava:777:WARN ] Running the getPartyFromUserLogin Service...
[java] 2012-03-27 14:47:28,861 (http-0.0.0.0-8080-1) [ PartyServices.
ava:792:INFO ] PartyFromUserLogin number found: 2
[java] 2012-03-27 14:47:28,865 (http-0.0.0.0-8080-1) [ RequestHandler.
ava:641:INFO ] Ran Event [xmlrpc:#] from [request], result is [null]
[java] 2012-03-27 14:47:28,866 (http-0.0.0.0-8080-1) [ ControlServlet.
ava:328:INFO ] [[[xmlrpc] Request Done- total:0.056,since last([xmlrpc] Request
...):0.056]]
你可以嘗試運行您的程序與
Iterator it = parties.iterator();
while(it.hasNext()){
System.out.println("ok : "+ it.next().getClass());
}
並發布結果?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.