Web service instance-Weather Forecast

Source: Internet
Author: User
Tags wsdl




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.




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.