Introduction to OFBiz WebService

Source: Internet
Author: User
Introduction to OFBiz WebService

 

In opentaps (later than OFBiz 9.04), the WebService uses axis2. At first, I searched a lot of information on the Internet and took it back for testing. I found that none of them was correct. Later, I searched for the Axis document and said that the error may be caused by incorrect versions. So I decided to see which version was used in OFBiz. At that time, I was completely speechless, the packages in both versions actually exist. I really don't know what it means. However, I think axis2 and OFBiz should not be able to use racks that will not be updated in.

Let's just talk about the development steps:

I. Introduce axis2 IN THE PROJECT. Because axis2 has several dependent packages, there should be no more clients, but just in case, we add all the jar packages in the lib directory under axis2 to the classpath of the development project.

 

 

 

2. To develop a WebService, you must know the WSDL. First, open a service in OFBiz so that it cannot be published as a WebService. This is very simple. In OFBiz, you only need to add an attribute export = "true" to the service to be published in the XML defined by the service, and then restart the service to see it. The following is an example:

<! -- [If! Supportlists] --> ① <! -- [Endif] -->: Find application/order/servicedef/services. open the XML file and find the last service. Here there is a built-in soap Test Service. We add a service, export = "true", and finally:

 

Method JavaCodeIs:

 

<! -- [If! Supportlists] --> ② <! -- [Endif] -->: now it is only released, but we must know how to request the service. OFBiz provides an event to process it, is

 

<! -- [If! Supportlists] --> ③ <! -- [Endif] --> restart the service and enter

Http: // localhost: 8080/ordermgr/control/soapservices/testsoap? WSDL. If you see the service you just released, it means it is successful. For example:

 

3. write on the client.

Client code writing mainly needs to know what the server wants. First, we can see that the definition of our service class requires one input output, one input, and three outputs, that is, two inputs and two outputs. In the WSDL, we can see that

 

 

We need to input a map-map model, and salt and userid are required. Because verification information is not set, login. username and login. Password are optional. The code is better.

During the test, I just made a simple example. Because axiom is complicated, I need to study it in detail. Class description: clientgenricvalue, which encapsulates the type, key, and value of the data to be imported. 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. omw.actfactory;

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 = omdomainactfactory. 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 = new options ();

 

Endpointreference targeterp = new endpointreference (endpoint); // define the destination endpointreference, which is the service address.

Options. setto (targeterp );

Options. settimeoutinmilliseconds (400000); // defines timeout, which is not defined here

 

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"); // This can be left blank

 

ValueList. Add (value1 );

ValueList. Add (value2 );

// ValueList. Add (value3 );

Return ValueList;

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.