Developing a Simple Web Service Client in Java
This section of the site contains additional content that didn't make it into the book or links to additional material published elsewhere.
Although Internet Services have Been in existence for some time (a long time at that)I haven't had the chance to really integrate any in an application I have worked . You might be in precisely the exact same ship, and so to the edification (and mine) I'll help you through developing a simple client that questions a web service using Java. Here goes . The very first step is to recognize an internet service which you'd love to use on your own application. It might be one which was made by a different team in your business, however in this case we will just select one out of a list of publicly available web services situated at www.xmethods.org. For this exercise I'll use the service referred to as"US Zip Validator". You may see the details of the service by clicking"Complete List" and performing a fast browser search. The WSDL for this service may be considered at http://www.w3schools.com/xml/xml_wsdl.asp. This ceremony, provided a US zip code, returns a State Abbreviation in addition to the latitude and longitude (of this"centre" of this zip code). The next step is to utilize the Java"wsimport" tool which ships with your JDK (Java Development Kit) to create the stub classes which will be utilized send and receive SOAP messages in the Internet services. I am using JDK version 1.6.0_33 however I think JDK 1.5+ ships using this tool. If you do not have a current JDK installed maybe it is time to update? Then run the following command: wsimport -s src -> bin http://www.webservicemart.com/uszip.asmx?WSDL This command generates Java source code from the /src directory and then compiled .class files from the /bin directory relative to the directory where the command has been issued. These classes that are created will be utilized to get the remote web services. Be certain that you bring the freshly created zipws.jar in addition to the essential jars for the customer code. JDK 1.6 comprises all the essential jars needed to run this code. If you're using a previous JDK version, it is possible to assess Jar Finder to ascertain which jars include the essential packages in the import statements on lines 2-5 below. As soon as your development environment is about to proceed, produce a Java Class known as"ZipWSTest" together with the following code to do the web service and extract the most vital pieces of advice it returns.
Lines Depicted above, instantiate courses that use JAX-WS (Java API for XML Internet For this phone, it turns Out the web service returns one xml node: . So I Want to pad the answer with markup (line 12) to produce the series a well-formed XML file prepared for parsing. The Rest of this code (lines 15-31) just parses the XML to an in-memory Shrub and retrieves the feature values to publish to the console. The closing Output can be observed from the Eclipse console window at the screen shot only below.