Cátedra PragmaEjercicio - Creando un Webservice a partir de un WSDL
Pre requisitos• Instalar jdk 1.6
• Instalar netbeans
• Instalar soapui
Problema • Vamos a crear un webservice a partir de un wsdl (contrato de
servicio)
1. Creemos un xsd y le damos el nombre de webservices.xsd con el siguiente contenido:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:ns1="http://blog.jdevelop.eu/soapwebservices.xsd" xmlns:tns="soapwebservices.jdevelop.eu"
xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="soapwebservices.jdevelop.eu" version="1.0">
<xs:element name="calculateValues">
<xs:complexType>
<xs:sequence>
<xs:element name="value1" type="xs:decimal"/>
<xs:element name="value2" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="calculateValuesResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="result" type="xs:decimal"/>
<xs:element name="errormessage" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
1. Creemos un wsdl y le damos el nombre de webservices.wsdl con el siguiente contenido. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns:ns1="soapwebservices.jdevelop.eu" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" name="SOAPWebServices" targetNamespace="soapwebservices.jdevelop.eu">
<types>
<xsd:schema>
<xsd:import namespace="soapwebservices.jdevelop.eu" schemaLocation="webservices.xsd"/>
</xsd:schema>
</types>
<message name="calculateValues">
<part name="calculateValues" element="ns1:calculateValues"/>
</message>
<message name="calculateValuesResponse">
<part name="calculateValuesResponse" element="ns1:calculateValuesResponse"/>
</message>
<portType name="SOAPWebServices">
<operation name="getCalculateValues">
<input message="ns1:calculateValues"/>
<output message="ns1:calculateValuesResponse"/>
</operation>
</portType>
<binding name="SOAPWebServicesPortBinding" type="ns1:SOAPWebServices">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getCalculateValues">
<soap:operation soapAction="urn:http://blog.jdevelop.eu/services/getCalculateValues"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="SOAPService">
<port name="WebServices" binding="ns1:SOAPWebServicesPortBinding">
<soap:address location="http://blog.jdevelop.eu:80/services"/>
</port>
</service>
</definitions>
3. Creamos una nueva aplicación en netbeans (File/new projects)
4. Le ponemos de nombre “SOAPWebServices”, luego presionamos “next”
5. Limpiamos el campo “context path”, luego presionamos “finish”
6. Creamos un nuevo webservice a partir de un wsdl (file, new file...), luego “next >”
6. Nombramos la clase que implementa el webservice como “ServiceImpl” y como paquete ponemos “eu.jdevelop.soapwebservices.service”. Seleccionamos el archivo wsdl creado anteriormente. Presionamos “finish”
7. Abrimos la clase ServiceImpl.java y modificamos el método getCalculateValues, con el codigo: eu.jdevelop.soapwebservices.CalculateValuesResponse res = new eu.jdevelop.soapwebservices.CalculateValuesResponse();
res.setResult(calculateValues.getValue1().add(calculateValues.getValue2()));
return res;
8. Ejecutamos la aplicación Web y el webservice en Netbeans, con click derecho sobre el proyecto SOAPWebServices/run
10. Ejecutar la url http://localhost:8080/SOAPService
11. En soapui crear un nuevo proyecto a partir del wsdl “http://localhost:8080/SOAPService?wsdl” y verificar su funcionamiento
Nombre: Juan Fernando Londoño CorreaE-mail: [email protected]: http://www.pragma.com.co/catedra_pragma
Top Related