Second internship -- java client calls webservice

Source: Internet
Author: User

During my internship, I mainly used the axis2 tool to implement java webservice calls.
One of the two methods I commonly use is to directly use the invokeBlocking method of RPCServiceClient, and the other is the createOMElement method of OMAbstractFactory. In fact, the two methods are essentially the same as processing xml. Because the cornerstone of webservice is xml. If you use a packet capture tool to view the transmission information of the relevant transmission protocol, you will find that, in fact, the execution process of these two methods is to call the relevant webservice method by transmitting xml data to each other.
The main process is that java receives the xml Information of webservice through the above method, and then fills in the parameter information of the calling method in xml, and then the webservice server uses the parameter you fill in, execute the corresponding method and return the corresponding data to xml. Then, the java client parses the data and obtains the corresponding data. This is a simple execution process.
After you know the execution process, let's talk about the specific use of the two methods.
Import jar is required first, or axis2 jar. It is relatively simple.
Method 1:
[Java]
RPCServiceClient serviceClient = newRPCServiceClient ();
 
Optionsoptions = serviceClient. getOptions ();
 
EndpointReferencetargetEPR = new EndpointReference (url );
 
Options. setTo (targetEPR );
 
// When creating a QName object, the first parameter of the constructor of the QName class indicates the namespace name of the WSDL file, that is, the targetNamespace attribute value of the <wsdl: definitions> element.
 
QName opAddEntry = new QName ("http://service.wf.cypheta.com", method );


 
This is mainly used to create a RPCServiceClient client object. Through this object, we can call the corresponding interface by passing in the corresponding webservice address. The above url is the url of the webservice to be passed in. Then we will create a QName object. In fact, QName is actually a xml element with a specific format (namespace + specific element ), after passing in the namespace and specific elements (the method we want to call here), we can locate the xml file information of the method we want to execute.
Then, the information about the input parameters and returned values is defined:

[Java]
<Span style = "color: black;"> Object [] opAddEntryArgs = new Object [] {481L };
 
// Return parameter type, which is a little different from axis1
 
// The invokeBlocking method has three parameters. The first parameter is a QName object, indicating the name of the method to be called;
 
// The second parameter indicates the parameter value of the WebService method to be called. The parameter type is Object [].
 
// The third parameter indicates the Class Object of the WebService method's return value type. The parameter type is Class [].
 
// When the method does not have a parameter, the second parameter value of the invokeBlocking method cannot be null, but new Object [] {} must be used.
 
// If the called WebService method does not return a value, use the invokeRobust method of the RPCServiceClient class,
 
// This method has only two parameters. Their meanings are the same as those of the first two parameters of the invokeBlocking method.
 
Class [] classes = new Class [] {String. class };
 
/* Object [] object = serviceClient. invokeBlocking (opAddEntry, opAddEntryArgs, classes );
 
For (int I = 0; I <object. length; I ++ ){
 
System. out. println (object [I]);
 
}*/
 
System. out. println (serviceClient. invokeBlocking (opAddEntry, opAddEntryArgs, classes) [0]. toString (); </span>


Finally, an object array is obtained through the invokeBlocking method, which encapsulates xml interaction operations for me, so we can easily call the webservice Interface and obtain our corresponding data.
 
Method 2:
This method is mainly to implement xml interaction by yourself: This includes four parts: 1. Set the URL for sending the request; 2. Set the webservice address of the call; 3. Get the returned xml data. 4. Use dom4j to parse the data returned by webservice.
Step 1:
[Java]
OMFactory fac = OMAbstractFactory. getOMFactory ();
 
OMNamespaceomNs = fac. createOMNamespace ("http://www.primeton.com/WSProcessInstManagerService", ""); // namespace
 
OMElementdata = fac. createOMElement (method, omNs); // obtain the name of the method to be called.
 
If (params. length! = 0 ){
 
For (int I = 0; I <params. length; I ++ ){
 
OMElementinner = fac. createOMElement (params [I], omNs); // obtain the parameter name to be called for this method name
 
Inner. setText (String. valueOf (paramValues [I]); // enter the Parameter
 
Data. addChild (inner );
 
}
 
}
 
// Add this parameter to the method node to be called
 
Return data;


This step mainly sets the url for sending the request, that is, the method we want to call and the parameters to be passed in. We can see from the code above that, first, we need to create such an xml file. The main data contained is the namespace, method name, and method parameter information.
Step 2:
[Java]
Options options = newOptions ();
 
Options. setSoapVersionURI (SOAP11Constants. SOAP_ENVELOPE_NAMESPACE_URI );
 
Options. setAction ("http://www.primeton.com/WSProcessInstManagerService" + method); // set the namespace plus method for the call
 
Options. setTo (targetAirline );
 
Options. setTransportInProtocol (Constants. TRANSPORT_HTTP); // you can specify the transport protocol.
 
Return options;


This part mainly sets the webservice address to be called. This step serves to set the url of the webservice to be called and the first three sentences of the above method.
Step 3:
[Java]
Try {
 
ServiceClientsender = new ServiceClient ();
 
Sender. setOptions (buildOptions (method ));
 
OMElementresult = sender. sendReceive (buildParam (params,
 
ParamValues, method ));
 
System. out. println ("parsed data:" + result. toString ());
 
Returnresult. toString ();
 
} Catch (Exception e ){
 
E. printStackTrace ();
 
System. out. println ("call error! ");
 
Return "call error! ";
 
}


This step is mainly to call related methods, and send the xml information we have set up to send to our webservice. webservice parses our xml request and executes the corresponding method, returns the corresponding information in xml format.
Last step:
[Java]
Try {
 
Documentdoc = incluenthelper. parseText (getResultByCode (params,
 
ParamValues, method ));
 
Elementroot = doc. getRootElement ();
 
Elementrn = root. element ("s"); // node name
 
System. out. println (rn. getData ());
 
Return (String) rn. getData ();
 
} Catch (incluentexception e ){
 
// TODOAuto-generated catch block
 
E. printStackTrace ();
 
System. out. println ("parsing error! ");
 
Return "parsing error! ";
 
}


This step is to use dom4j to parse the data we need. This is not detailed.
 
After this step, we successfully called webservice through java. In fact, it is not very difficult. The main reason is to understand how to handle the process.
 
We hope you can use axis2 to create a webservice and use axis2 to call webservice!

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.