Java Access WebService Interface (RPM)

Source: Internet
Author: User

Java calls WebService interface several methods

The release of WebService is generally published using the style of the WSDL (Web Service descriptive language) file, inside the WSDL file, which contains the interface that the webservice exposes for use on the external side. Search today for a very good list of WebService provider

Http://www.webservicex.net/WCF/default.aspx

This list contains more than 70 free WebService Provider,utilities->global weather that can be used to get global weather forecasts.

Let's look at how Java uses WSDL files to invoke these Web service:

Note that the following code is not really tested, just to illustrate these situations, different versions of axis vary greatly, it is best to use the Apache site example, this is only used to illustrate its basic usage.

1, direct axis calls the remote Web service

I think this method is more suitable for those masters, they can directly read the XML format of the WSDL file, I do not understand, especially I am not specifically engaged in this line, even if a period of time to understand, and then forget. The direct invocation pattern is as follows:

Importjava.util.Date;ImportJava.text.DateFormat;ImportOrg.apache.axis.client.Call;ImportOrg.apache.axis.client.Service;ImportJavax.xml.namespace.QName;ImportJava.lang.Integer;ImportJavax.xml.rpc.ParameterMode; Public classcaclient { Public Static voidMain (string[] args) {Try{String Endpoint= "HTTP://LOCALHOST:8080/CA3/SERVICES/CASYNROCHNIZED?WSDL"; //directly referencing a remote WSDL file//here are the routines.Service service =NewService (); Call Call=(call) Service.createcall ();            Call.settargetendpointaddress (endpoint); Call.setoperationname ("AddUser");//interface name described in WSDLCall.addparameter ("UserName", Org.apache.axis.encoding.XMLType.XSD_DATE, Javax.xml.rpc.ParameterMode.IN);//parameters of the interfaceCall.setreturntype (Org.apache.axis.encoding.XMLType.XSD_STRING);//Set return typeString Temp= "Test Person"; String result= (String) Call.invoke (Newobject[]{temp}); //pass a parameter to the method, and call the methodSystem.out.println ("Result is" +result); }        Catch(Exception e) {System.err.println (e.tostring ()); }    }}

2, direct SOAP calls remote WebService

I've never seen or tried this pattern, but someone posted it on the internet and I turned around.

ImportOrg.apache.soap.util.xml.*;Importorg.apache.soap.*;ImportOrg.apache.soap.rpc.*;ImportJava.io.*;Importjava.net.*;ImportJava.util.Vector; Public classcaservice{ Public Staticstring GetService (string user) {URL URL=NULL; Try{URL=NewURL ("http://192.168.0.100:8080/ca3/services/caSynrochnized"); } Catch(malformedurlexception Mue) {returnMue.getmessage (); }        //This is the main SOAP objectCall Soapcall=NewCall (); //Use SOAP encodingSoapcall.setencodingstyleuri (CONSTANTS.NS_URI_SOAP_ENC); //This is the remote object we ' re asking for the priceSoapcall.settargetobjecturi ("Urn:xmethods-casynrochnized"); //The the name of the the method on the above objectSoapcall.setmethodname ("GetUser"); //We need to send the ISBN number as a input parameter to the methodVector Soapparams=NewVector (); //name, type, value, encoding styleParameter Isbnparam=NewParameter ("UserName", String.class, User,NULL);        Soapparams.addelement (Isbnparam);        Soapcall.setparams (Soapparams); Try {            //Invoke The remote method on the objectResponse Soapresponse= Soapcall.invoke (URL, ""); //Check to see if there are an error, return "N/a"            if(Soapresponse.generatedfault ()) {Fault Fault=Soapresponse.getfault (); String F=fault.getfaultstring (); returnF; } Else {                //Read resultParameter Soapresult=Soapresponse.getreturnvalue (); //get a string from the result                returnSoapresult.getvalue (). toString (); }        } Catch(SoapException se) {returnSe.getmessage (); }    }}

3, use Wsdl2java to convert the WSDL file to a local class, and then use it as a local class.

This is a favorite way for lazy people like me, still taking the example of the previous global Weather report.

First Java Org.apache.axis.wsdl.WSDL2Java Http://www.webservicex.net/globalweather.asmx.WSDL

The original URL is Http://www.webservicex.net/globalweather.asmx?WSDL, the middle of each question mark, but Linux below it can not parse, so remove the question mark, change to the point number.

Then there will be 4 files:

Globalweather.java Globalweatherlocator.java Globalweathersoap.java Globalweathersoapstub.java
Where Globalweathersoap.java is our most concerned interface file, if you are not interested in the specifics of the SOAP implementations such as RMI, then you just need to look at the interface file and, when used, introduce this interface as if you were using a local class.

Java Access WebService Interface (RPM)

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.