Talk about WebService Development-application (ii)-based on cxf-weather forecast

Source: Internet
Author: User
Tags soap wsdl

We tried to invoke the free WebService service from the source JDK, which we tried to use the Apache WebService Open source framework CXF to develop a client-requested weather forecast.

Development steps:

1. Create client project: weather-cxf-client, download CXF related jar package as tools, such as, need friends can download online or small window me;

2. Using the CXF tool Wsdl2java.bat generate the client code, the tool in the following directory, considering the ease of use, the following bin directory is set to the system environment variables:

3. Find the WSDL address of the free weather Forecast service, find the method that has been provided in the previous article, check the following address: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

4. Enter the SRC directory of the project created in step 1th to execute Wsdl2java-keep http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl, Executing this command will report an error such as:

The reason for this problem in the previous comment is that because the server side of this weather forecast is developed with net, there are some problems when Java calls Net development webservice, which can be solved by the following method, now let's verify:

A. First generate the local WSDL document, browser access HTTP://WEBSERVICE.WEBXML.COM.CN/WEBSERVICES/WEATHERWS.ASMX?WSDL will display the contents of the copy saved to a local file weather.wsdl can;

B. Place the weather.wsdl in this local document <s:element ref= "S:schema"/><s:any/> replaced by <s:anyminoccurs= "2" maxoccurs= "2"/>;

C. Generate the client calling code in the local WSDL file using the previously described method, executing and generating the following results:

Perform:


Results:

5. Write the calling test code:

Package Com.devins.ws.weather;import Java.util.list;import Cn.com.webxml.arrayofstring;import Cn.com.webxml.weatherws;import Cn.com.webxml.weatherwssoap;public class Weatherquerytest {public static void main ( String[] args) {Weatherws factory = new Weatherws (); Weatherwssoap Wssoap = Factory.getweatherwssoap (); Arrayofstring weather = wssoap.getweather ("Guangzhou", null); list<string> weatlist = weather.getstring (); for (String temp:weatlist) {System.out.println (temp);}}}

Execution Result:

Guangzhou 23502015/05/08 08:54:27 Weather today: temperature: 26 ℃; wind/Wind: South wind 3 level; Humidity: 83% Sunglasses index: Hot. Suitable for summer wear such as T-shirts and short coats. May 8 Cloudy 24 ℃/31 ℃ No continuous wind Breeze 1.gif1.gif5 month 9th to heavy rain 23 ℃/28 ℃ No continuous wind breeze 23.gif9.gif5 month 10th Cloudy 23 ℃/28 ℃ No continuous wind breeze 1.GIF1.GIF5 month 11th Heavy rain Turn thunderstorm 23 ℃/28 ℃ No continuous wind breeze 23.gif4.gif5 month 12th Cloudy 24 ℃/29 ℃ No continuous wind breeze 1.gif1.gif

Note that this client-side engineering call does not require the introduction of CXF's related jar package, and the introduction of the above test code will report the following error:

Exception in thread "main" Javax.xml.ws.soap.SOAPFaultException:No namespace on "html" element. You must send a SOAP request. At Org.apache.cxf.jaxws.JaxWsClientProxy.invoke (jaxwsclientproxy.java:156) at Com.sun.proxy. $Proxy 32.getWeather ( Unknown Source) at Com.devins.ws.weather.WeatherQueryTest.main (weatherquerytest.java:18) caused by: Org.apache.cxf.binding.soap.SoapFault:No namespace on "html" element. You must send a SOAP request. At Org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.readVersion (readheadersinterceptor.java:109) At Org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage (readheadersinterceptor.java:142 ) at Org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage (readheadersinterceptor.java:60 ) at Org.apache.cxf.phase.PhaseInterceptorChain.doIntercept (phaseinterceptorchain.java:263) at Org.apache.cxf.endpoint.ClientImpl.onMessage (clientimpl.java:801) at org.apache.cxf.transport.http.httpconduit$ WrappedOutputStream.hAndleresponseinternal (httpconduit.java:1679) at org.apache.cxf.transport.http.httpconduit$ Wrappedoutputstream.handleresponse (httpconduit.java:1517) at org.apache.cxf.transport.http.httpconduit$ Wrappedoutputstream.close (httpconduit.java:1425) at Org.apache.cxf.transport.AbstractConduit.close ( abstractconduit.java:56) at Org.apache.cxf.transport.http.HTTPConduit.close (httpconduit.java:650)

The next article will delve into the analysis and illustration of the WSDL document.


Talk about WebService Development-application (ii)-based on cxf-weather forecast

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.