Originally, you could use myeclipse to automatically generate a client, and then simply call the client. However, during the creation process, the following problem occurs:
Figure 2.3.1 construct a web service client
The reason is that myeclipse cannot parse tags. For more information, see the WSDL documentation provided by the service provider:
Xmlns: S = http://www.w3.org/2001/XMLSchema
This sentence cannot be parsed by myeclipse. For this reason, we can only create it through the command line and bind the XSD file during the build process.
In fact, this file is the content of JDK, and Java has been abandoned and no longer used, which causes its failure to pass verification.
We now build a Java project in myeclipse (because we only want to test, and do not necessarily need a WEB Project ):
Figure 2.3.2 build a Java Project
Figure 2.3.3 add JAX-WS support
Open the project, enter the src directory, and run the CMD build command:
Wsimport-P com. Test. Client
-Keep http://www.webxml.com.cn/WebServices/WeatherWebService.asmx? WSDL
-B
Http://www.w3.org/2001/XMLSchema.xsd
Note: It must be written at one time.
-P: Package
-Keep: the URL of the WSDL document
-B: the schema file is missing.
Figure 2.3.4 enter the project directory SRC
Figure 2.3.5 parse the WSDL document
Figure 2.3.6 complete parsing with a warning
Note: a warning is reported for parsing. This is normal, but we have added the missing ones, so we don't have to worry about this. Let's take a look at our project and refresh the projects in myeclipse:
Figure 2.3.7 file structure
We can see that it automatically creates the com. Test. Client package, which contains a series of automatically generated Java classes. So far, our client has been created.
Now we add a test class: webweatherclient
Package COM. test. client; public class webweatherclient {public static void main (string [] ARGs) {weatherwebservice service = new weatherwebservice (); weatherwebservicesoap = service. getweatherwebservicesoap (); system. out. println (soap. getweatherbycityname ("Hangzhou "). getstring ());}}
Code segment 2.3.1 test class.
Now let's run:
We can see that we have obtained the weather information of the city Hangzhou.