Java Reference
In-Depth Information
url = new URL(" http://www.webservicex.net/stockquote.asmx?WSDL");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
STOCKQUOTE_WSDL_LOCATION = url;
STOCKQUOTE_EXCEPTION = e;
}
Note The code snippet that shows the generated StockQuote class
shows a so‐called “static block,” which you have not worked with explic-
itly before. Static code blocks are helpful when you want to instantiate
some static variables, but need to catch some error conditions with a
try-catch block as well, as can be observed here. Just typing the follow-
ing would not work, as Java expects you to catch a possible exception—a
MalformedURLException —which can occur during the instantiation of an URL
object:
private final static URL STOCKQUOTE_WSDL_LOCATION = new
URL(" http://www.webservicex.net/stockquote.asmx?WSDL");
To speed up the code somewhat, and to prevent the code from breaking down when the WSDL file is
unavailable (but the SOAP service itself still works), it's also possible to store this file on your local
machine.
To do so, navigate to http://www.webservicex.net/stockquote.asmx?WSDL in your web
browser and save this file somewhere, for instance on your desktop, as stockquote.wsdl . Next,
in Eclipse, right‐click the SOAPWithJava project in the package explorer (or on your project name
if you're using a different one) and create a new “Folder” (not a “Source Folder”). Call it wsdl . See
Figure 10-16.
Next, drag your saved stockquote.wsdl file over to the wsdl folder in the package explorer and
create a copy (you may now remove your originally saved file). Next, edit the code in StockQuote to
look like the following:
static {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
WebServiceException e = null;
URL url = classloader.getResource("wsdl/stockquote.wsdl");
STOCKQUOTE_WSDL_LOCATION = url;
STOCKQUOTE_EXCEPTION = e;
}
The generated class will now use the locally stored WSDL file instead of retrieving it online.
You have now seen how to access SOAP web services, both by constructing SOAP messages
manually and by using the wsimport tool to automatically generate classes. Obviously, in most
cases, the functionality offered by wsimport is the recommended way to write clients using SOAP
Search WWH ::




Custom Search