lunes, 19 de abril de 2010

Web Service Client con Netbeans(SOAP)

Ahora voy a demostrar como crear un Web Service Client desde Netbeans, para este ejemplo estoy utilizando NetBeans 6.1, JDK1.6 y Ubuntu 9.10:
Primeramente Abrimos Netbeans y creamos un proyecto Java(El mismo ejemplo lo podriamos hacer desde una aplicacion web). Esta aplicación la llamamos como WSClient.



Damos click derecho sobre el proyecto y Seleccionamos la categoria Web Services y en File Types Seleccionamos Web Service Client, damos click sobre Next


En la siguiente pantalla marcamos WSDL URL y colocamos la siguiente URL:
http://www.webservicex.net/globalweather.asmx?wsdl y damos click en Finish(Dejamos el estilo del cliente como JAX-WS Client).
Una vez hecho esto, en el lado izquierdo veremos el recurso GlobalWeather dentro de Web Service References.




Ahora, dentro de public static void main, de Main.java damos boton derecho y seleccionamos la opción "Web Service Cliente Resorce" y damos click en "call web service operation". Se nos abre una ventana con los diferentes métodos del Web Service de Global Weather.


Seleccionamos GetWeather y damos click en OK

y nos agrega el siguiente código
try { // Call Web Service Operation
net.webservicex.GlobalWeather service = new net.webservicex.GlobalWeather();
net.webservicex.GlobalWeatherSoap port = service.getGlobalWeatherSoap();
// TODO initialize WS operation arguments here
java.lang.String cityName = "Quito";
java.lang.String countryName = "Ecuador";
// TODO process result here
java.lang.String result = port.getWeather(cityName, countryName);
System.out.println("Result = " + result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}


Podemos cambiar las variables cityName y countryName a Quito y Ecuador respectivamente y ejecutar el aplicativo, con lo cual tenemos una salida como esta:

Result =
Quito / Mariscal Sucre, Ecuador (SEQU) 00-09S 078-29W 2812M

Variable at 2 MPH (2 KT):0
greater than 7 mile(s):0
mostly cloudy
66 F (19 C)
51 F (11 C)
59%
30.42 in. Hg (1030 hPa)
Success

No hay comentarios:

Publicar un comentario