Java calls WebService interface several methods

Source: Internet
Author: User

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:

[Java]View PlainCopy
  1. Import Java.util.Date;
  2. Import Java.text.DateFormat;
  3. Import Org.apache.axis.client.Call;
  4. Import Org.apache.axis.client.Service;
  5. Import Javax.xml.namespace.QName;
  6. Import Java.lang.Integer;
  7. Import Javax.xml.rpc.ParameterMode;
  8. Public class Caclient {
  9. public static void Main (string[] args) {
  10. try {
  11. String endpoint = "HTTP://LOCALHOST:8080/CA3/SERVICES/CASYNROCHNIZED?WSDL";
  12. //Direct reference to the remote WSDL file
  13. //The following are routines
  14. Service service = new service ();
  15. Call Call Service.createcall ();
  16. Call.settargetendpointaddress (endpoint);
  17. Call.setoperationname ("AddUser"); Interface name described in WSDL
  18. Call.addparameter ("UserName",
  19. Org.apache.axis.encoding.XMLType.XSD_DATE,
  20. Javax.xml.rpc.ParameterMode.IN); //Parameters of the interface
  21. Call.setreturntype (Org.apache.axis.encoding.XMLType.XSD_STRING); //Set return type
  22. String temp = "tester";
  23. String result = (string) call.invoke (new object[] {temp});
  24. //Pass parameters to method, and call method
  25. System.out.println ("result is" + result);
  26. } catch (Exception e) {
  27. System.err.println (E.tostring ());
  28. }
  29. }
  30. }



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.

[Java]View PlainCopy
  1. Import org.apache.soap.util.xml.*;
  2. Import org.apache.soap.*;
  3. Import org.apache.soap.rpc.*;
  4. Import java.io.*;
  5. Import java.net.*;
  6. Import Java.util.Vector;
  7. Public class Caservice {
  8. public static string GetService (string user) {
  9. URL url = null;
  10. try {
  11. url = new URL (
  12. "http://192.168.0.100:8080/ca3/services/caSynrochnized");
  13. } catch (Malformedurlexception Mue) {
  14. return Mue.getmessage ();
  15. }
  16. //This is the main SOAP object
  17. Call Soapcall = new Call ();
  18. //Use SOAP encoding
  19. Soapcall.setencodingstyleuri (CONSTANTS.NS_URI_SOAP_ENC);
  20. //This is the remote object we ' re asking for the price
  21. Soapcall.settargetobjecturi ("urn:xmethods-casynrochnized");
  22. The the name of the the method on the above object
  23. Soapcall.setmethodname ("GetUser");
  24. //We need to send the ISBN number as a input parameter to the method
  25. Vector soapparams = new vector ();
  26. //Name, type, value, encoding style
  27. Parameter Isbnparam = new Parameter ("UserName", String. class, user,
  28. null);
  29. Soapparams.addelement (Isbnparam);
  30. Soapcall.setparams (Soapparams);
  31. try {
  32. //Invoke The remote method on the object
  33. Response soapresponse = Soapcall.invoke (URL, "");
  34. //Check to see if there are an error, return "N/a"
  35. if (Soapresponse.generatedfault ()) {
  36. Fault Fault = Soapresponse.getfault ();
  37. String f = fault.getfaultstring ();
  38. return F;
  39. } Else {
  40. //Read result
  41. Parameter Soapresult = Soapresponse.getreturnvalue ();
  42. //Get a string from the result
  43. return Soapresult.getvalue (). toString ();
  44. }
  45. } catch (SoapException se) {
  46. return Se.getmessage ();
  47. }
  48. }
  49. }

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 SOAP implementations such as RMI, then you just need to look at the interface file,

When used, this interface is introduced as if it were a local class.

Java calls WebService interface several methods

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.