转 OFBIZ webservice简介
OFBIZ webservice簡(jiǎn)介
?
Opentaps(OFBiz 9.04之后)中webservice用的是AXIS2,最開始自己在網(wǎng)上搜了好多資料,自己拿回來測(cè)試,發(fā)現(xiàn)都不對(duì)。后自己再找了下AXIS的資料說,那種報(bào)錯(cuò)很有可能是由于兩個(gè)版本不對(duì)引起的,所以就決定看看OFBiz里面用的是哪個(gè)版本,當(dāng)時(shí)我徹底無(wú)語(yǔ)了,里面兩個(gè)版本的包竟然都有,真不知道是什么意思。但是我認(rèn)為應(yīng)該是AXIS2,OFBiz這么與時(shí)俱進(jìn)的東西,應(yīng)該不太可能用06年就不更新的架包。
廢話少說,直接說開發(fā)步驟吧:
一:在項(xiàng)目中引入AXIS2,由于AXIS2的依賴包好幾個(gè),客戶端應(yīng)該不需要那么多,但是以防萬(wàn)一,我們把AXIS2下面lib目錄的所有jar包一并加入到開發(fā)工程的classpath下。
?
?
?
二:開發(fā)webservice必須知道wsdl才能比較好的。首先我們?cè)贠FBiz中打開一個(gè)service,讓它能被發(fā)不成webservice。這個(gè)非常簡(jiǎn)單,在OFBiz中你只要在service定義的xml中,把要發(fā)布的service添加一個(gè)屬性export=”true”,重啟服務(wù)就能看到。下面以一個(gè)實(shí)例來說明:
<!--[if !supportLists]-->① <!--[endif]-->:我們找到application/order/servicedef/services.xml文件,打開找到最后一個(gè)service,這里有個(gè)自帶的SOAP測(cè)試服務(wù),我們添加一個(gè)service,export="true"加上去,最后變成:
?
方法java代碼為:
?
<!--[if !supportLists]-->② <!--[endif]-->:現(xiàn)在只是發(fā)布了,但是我們必須要知道怎樣請(qǐng)求才能得到這個(gè)服務(wù),ofbiz提供了一個(gè)event來處理它,就是<handler name="soap" type="request" class="org.ofbiz.webapp.event.SOAPEventHandler"/>,要使用它,你必須把這個(gè)定義在你的controller.xml文件中,當(dāng)然,如果你已經(jīng)引入了<include location="component://common/webcommon/WEB-INF/common-controller.xml"/>,那么就不需要了,這個(gè)里面已經(jīng)定義好了。直接使用就行了。
?
<!--[if !supportLists]-->③ <!--[endif]-->重啟服務(wù),在瀏覽器中輸入
http://localhost:8080/ordermgr/control/SOAPServices/testSoap?wsdl,如果你看到了你剛才發(fā)布的服務(wù),說明已經(jīng)成功。如下圖:
?
三:客戶端編寫。
客戶端代碼編寫最主要是要知道服務(wù)端想要的是什么東西,首先我們的服務(wù)類的定義我們可以看到是需要一個(gè)輸入輸出,一個(gè)輸入,三個(gè)輸出的,也就是兩個(gè)進(jìn),兩個(gè)出。在wsdl中我們可以看到
?
?
說明我們需要傳入的是一個(gè)map-Map形式的Model,并且salt和userid是必須的。由于我們沒有設(shè)置驗(yàn)證信息,所以login.username和login.password是可以不需要的。直接上代碼比較好一些。
在測(cè)試的時(shí)候我僅僅弄了一個(gè)很簡(jiǎn)單的例子,由于axiom比較復(fù)雜,還要詳細(xì)研究下。類說明:ClientGenricValue,封裝的是要傳入數(shù)據(jù)的類型,鍵key和值。ClientUtil
package com.wx;
?
public class ClientGenericValue {
?
private String type;
private String key;
private String value;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
?
?
}
?
?
package com.wx;
?
import java.util.List;
?
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
?
public class ClientUtil {
?
private static OMFactory factory = OMAbstractFactory.getOMFactory();
private String endPoint;
private List<ClientGenericValue> valueList;
private String om;
private String serviceName;
?
public ClientUtil(String endPoint, String serviceName , String om,
List<ClientGenericValue> valueList) {
this.endPoint = endPoint;
this.serviceName = serviceName;
this.om = om;
this.valueList = valueList;
}
?
?
?
public ServiceClient createClient() throws AxisFault {
ServiceClient client = new ServiceClient();
Options options = new Options();
?
EndpointReference targetERP = new EndpointReference(endPoint); //定義目的EndpointReference,就是服務(wù)地址
options.setTo(targetERP);
options.setTimeOutInMilliSeconds(400000); //定義超時(shí),這里可以不定義
?
client.setOptions(options);
return client;
}
?
public OMElement send() throws AxisFault{
OMNamespace ns = factory.createOMNamespace("http://ofbiz.apache.org/service/", serviceName);
OMElement root = factory.createOMElement(serviceName, ns);
?
OMElement data = factory.createOMElement("map-HashMap", null);
root.addChild(data);
for(ClientGenericValue value : valueList){
OMElement mapKey = factory.createOMElement("map-Entry", null);
?
OMElement keyElement = factory.createOMElement("map-Key", null);
OMElement keyValue = factory.createOMElement("std-String", null);
keyValue.addAttribute(factory.createOMAttribute("value", null, value.getKey()));
keyElement.addChild(keyValue);
?
?
OMElement valueElement = factory.createOMElement("map-Value", null);
OMElement valueValue = factory.createOMElement(value.getType(), null);
valueValue.addAttribute(factory.createOMAttribute("value", null, value.getValue()));
valueElement.addChild(valueValue);
?
mapKey.addChild(keyElement);
mapKey.addChild(valueElement);
?
data.addChild(mapKey);
}
System.out.println(root);
OMElement result = createClient().sendReceive(root);
return result;
}
?
?
?
public String getEndPoint() {
return endPoint;
}
?
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
?
}
?
package com.wx;
?
import java.util.ArrayList;
import java.util.List;
?
import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
?
public class ClientTest {
?
/**
* @param args
* @throws AxisFault
*/
public static void main(String[] args) {
String endPoint = "http://localhost:8080/ordermgr/control/SOAPServices/testSoap";
String serviceName = "testSoap";
String om = "http://ofbiz.apache.org/service/";
List<ClientGenericValue> valueList = getTestData();
?
?
ClientUtil cu = new ClientUtil(endPoint, serviceName, om, valueList);
?
try {
OMElement result = cu.send();
System.out.println("Sent Hello, got : " + result.toString());
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
?
}
?
public static List<ClientGenericValue> getTestData(){
List<ClientGenericValue> valueList = new ArrayList<ClientGenericValue>();
ClientGenericValue value1 = new ClientGenericValue();
value1.setType("std-String");
value1.setKey("userid");
value1.setValue("11111");
?
ClientGenericValue value2 = new ClientGenericValue();
value2.setType("std-String");
value2.setKey("salt");
value2.setValue("The power of OFBiz");
?
// ClientGenericValue value3 = new ClientGenericValue();
// value3.setType("eeval-OrderItemTypeAttr");
// value3.setKey("testing");
// value3.setValue("org.ofbiz.entity.GenericValue"); //這個(gè)可以不用填寫的
?
valueList.add(value1);
valueList.add(value2);
// valueList.add(value3);
return valueList;
}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/Ivan-j2ee/archive/2012/12/12/2813920.html
總結(jié)
以上是生活随笔為你收集整理的转 OFBIZ webservice简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hadoop配置启动historyser
- 下一篇: 最棒的PHP后台管理系统