How does java call the webservice Interface?

Source: Internet
Author: User

How does java call the webservice Interface?

Java can directly use axis. jar provided by Apache to write code, or use Eclipse to automatically generate WebService Client code and call it using the Proxy class. In theory, it is the same, but it is easier to use Eclipse to automatically generate code.
1. Code Writing Method:
Package com. yudun. test;
Import java. rmi. RemoteException;
Import org. apache. axis. client. Call;
Import org. apache. axis. client. Service;
Import org. apache. axis. message. PrefixedQName;
Import org. apache. axis. message. SOAPHeaderElement;
Import com. cezanne. golden. user. Exception;
Import com. cezanne. golden. user. UserManagerServiceProxy;
Import javax. xml. namespace. QName;
Import java.net. MalformedURLException;
Import javax. xml. rpc. ServiceException;
Import javax. xml. soap. Name;
Import javax. xml. soap. SOAPException;

Public class testWebService {
Public static String getResult () throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
// Identify the specific path of the Web Service
String endpoint = "WebService address ";
// Create a Service instance
Service service = new Service ();
// Create a Call instance through a Service instance
Call call = (Call) service. createCall ();
// Add the Web Service path to the call instance.
Call. setTargetEndpointAddress (new java.net. URL (endpoint); // set the service location for the Call.
// Because authentication is required, you need to set the SOAP header information for the call.
Name headerName = new PrefixedQName (new QName ("url in targetNamespace in the published wsdl", "string_itemName "));
Org. apache. axis. message. SOAPHeaderElement header = new SOAPHeaderElement (headerName );
Header. addTextNode ("blablabla ");
Call. addHeader (header );

// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement ("url in targetNamespace in the published wsdl", "SoapHeader ");
// SoapHeaderElement. setNamespaceURI ("url in targetNamespace in the published wsdl ");
// Try
//{
// SoapHeaderElement. addChildElement ("string_itemName"). setValue ("blablabla ");
//}
// Catch (SOAPException e)
//{
// E. printStackTrace ();
//}
// Call. addHeader (soapHeaderElement );
// Call the Web Service Method
Org. apache. axis. description. OperationDesc restart;
Org. apache. axis. description. ParameterDesc param;
Topology = new org. apache. axis. description. OperationDesc ();
Response. setName ("opName ");
Param = new org. apache. axis. description. parameterDesc (new javax. xml. namespace. QName ("", "arg0"), org. apache. axis. description. parameterDesc. IN, new javax. xml. namespace. QName ("http://www.w3.org/2001/XMLSchema", "string"), java. lang. string. class, false, false );
Param. setOmittable (true );
Parameters. addParameter (param );
Param = new org. apache. axis. description. parameterDesc (new javax. xml. namespace. QName ("", "arg1"), org. apache. axis. description. parameterDesc. IN, new javax. xml. namespace. QName ("http://www.w3.org/2001/XMLSchema", "string"), java. lang. string. class, false, false );
Param. setOmittable (true );
Parameters. addParameter (param );
Param = new org. apache. axis. description. parameterDesc (new javax. xml. namespace. QName ("", "arg2"), org. apache. axis. description. parameterDesc. IN, new javax. xml. namespace. QName ("http://www.w3.org/2001/XMLSchema", "string"), java. lang. string. class, false, false );
Param. setOmittable (true );
Parameters. addParameter (param );
Criteria. setReturnType (new javax. xml. namespace. QName ("http://www.w3.org/2001/XMLSchema", "string "));
Response. setReturnClass (java. lang. String. class );
Response. setReturnQName (new javax. xml. namespace. QName ("", "return "));
Examples. setStyle (org. apache. axis. constants. Style. WRAPPED );
Examples. setUse (org. apache. axis. constants. Use. LITERAL );
Parser. addFault (new org. apache. axis. description. FaultDesc (
New javax. xml. namespace. QName ("url in targetNamespace in the published wsdl", "Exception "),
"Exception ",
New javax. xml. namespace. QName ("url in targetNamespace in the published wsdl", "Exception "),
True
));
Call. setOperation (operator );
Call. setOperationName (new javax. xml. namespace. QName ("url in targetNamespace in the published wsdl", "opName "));
// Call Web Service and input parameters
String res = (String) call. invoke (new Object [] ("arg0", "arg1 "));
System. out. println ("================== ");
Return res;
}
/**
* @ Param args
*/
Public static void main (String [] args ){
Try {
System. out. println (getResult ());
} Catch (MalformedURLException e ){
E. printStackTrace ();
} Catch (RemoteException e ){
E. printStackTrace ();
} Catch (ServiceException e ){
E. printStackTrace ();
} Catch (SOAPException e ){
E. printStackTrace ();
}
}
}

2. It is much easier to use Eclipse to automatically generate WebService client code: (because no image is sent, you can use the language to describe it. Let's take a look ...)
First, select other for the new project, enter the Web Service Client in the input box, select the search result, click Next, enter the WebService publishing address in Service definition, and click Finish.
In this way, the WebService Client code has been generated.
Next, write a Test class and enter the following code in the main function:

String endpoint = "WebService address of the server ";
YourWebServiceNameProxy umsp = new YourWebServiceNameProxy (endpoint );
Try {
String resultStr = umsp. opMethod ("arg0", "arg1 ");
System. out. println (resultStr );
} Catch (Exception e ){
System. out. println ("exception ");
E. printStackTrace ();
} Catch (RemoteException e ){
System. out. println ("RemoteException exception ");
E. printStackTrace ();
}

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.