Está en la página 1de 22

UNIVERSIDAD ALAS PERUANAS 10/12/2014

Introduccin a los Servicios


Web con Java
Lic. Vladimir Cotaquispe Gutierrez

E-mail: vcotagexamen@hotmail.com
Twitter: @vcotag

Servicios Web

Son la invocacin de cdigo remoto utilizando


protocolos estandarizados.

En conclusin, realizan la misma funcin que los


sockets, RPC, RMI, Corba y dems tecnologas
distribuidas.

Se puede ver a los servicios Web como una analoga


de un procedimiento almacenado en una base de
datos.

2 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 1


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Servicios Web

Los servicios Web van de la mano de las tecnologas XML.

XML nos sirve para estandarizar el marshalling de los datos.

Utilizar la Web nos permite tener un puerto no bloqueando por Firewall

3 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Qu son los Servicios Web?

"A Web service is a software system designed to support


interoperable machine-to-machine interaction over a network. It has
an interface described in a machine-processable format
(specifically WSDL). Other systems interact with the Web service in
a manner prescribed by its description using SOAP-messages,
typically conveyed using HTTP with an XML serialization in
conjunction with other Web-related standards."

4 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 2


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Caractersticas
La aplicacin que acta como cliente debe
conocer:
La URL del servidor remoto que
ofrece el servicio,
El nombre del servicio que se
solicita, y
Los parmetros que se deben
enviar junto con la llamada al
servicio.

Estos datos se enviarn mediante HTTP

5 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Caractersticas

El servidor que ofrece el servicio web leer los parmetros que se le


han enviado, llamar a un componente o programa encargado de
implementar el servicio, y los resultados que se obtengan de su
ejecucin sern devueltos al servidor que solicit la ejecucin del
servicio.

6 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 3


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Servicios Web

Los servicios Web forman la base de la


arquitectura orientada a servicios (SOA)

Los servicio Web utilizan generalmente el


mtodo POST de HTTP para enviar los datos
de la invocacin del servicio.

7 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Arquitectura de Servicios Web

Proveedor de Servicios

Servicio Conectar
Publicar

Registro de Servicios Solicitante de Servicio


Encontrar

Descripcin Cliente
8 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 4


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Esquema del funcionamiento de servicios web

registro

objRef Interfaz obj


comn

objRef.f(arg) obj.f(arg)

Cliente Servido
r

9 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Servicios Web

Los datos viajan envueltos en un protocolo llamado SOAP (Simple


Object Access Protcol) que hace el marshalling de los datos.

Una de las principales caractersticas que tienen los servicios Web


radica en su ubicuidad, ya que pueden ser accedidos desde
cualquier sitio, utilizando inclusive cualquier otro protocolo de
transporte SMTP, FTP, etc.

10 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 5


UNIVERSIDAD ALAS PERUANAS 10/12/2014

SOAP

Indica cmo se deben codificar los mensajes que circularn entre las
dos aplicaciones.

SOAP define dos modelos de mensajes:


Un mensaje de solicitud.
Un mensaje de respuesta.

11 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Mensaje de solicitud

<?xml version="1.0" encoding="UTF-8" ?>


<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<catalogo:buscaIsbn xmlns:catalogo="http://catalogo.org/cat">
<catalogo:isbn>
84-4553-3334-2X
</catalogo:isbn>
</catalogo:buscaIsbn>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

12 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 6


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Mensaje de respuesta

<?xml version="1.0" encoding="UTF-8" ?>


<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<catalogo:buscaIsbnResponse xmlns:catalogo="http://catalogo.org/cat">
<catalogo:titulo>
Catalogar materiales especiales
</catalogo:titulo>
<catalogo:autor>Marta de Juanes</catalogo:autor>
</catalogo:buscaIsbnResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
13 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Servicios Web
Los servicios Web necesitan ser descritos
(saber que parmetros reciben, devuelven)
para poderlos utilizar en diversos clientes.
Esta descripcin se realiza a travs de
WSDL (Web Service Definition Language).

Generalmente esas descripciones los


clientes las conocen o bien, puede
descubrirlas haciendo uso de UDDI
(Universal Description, Discovery and
Integration).

14 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 7


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Servicios Web

La UDDI no es otra cosa que un repositorio en donde se almacenan


servicios Web que pueden ser invocados por diversos clientes.

Muchas empresas ofrecen servicios Web como amazon, google,


http://www.xmethods.com

15 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Por qu utilizar Servicios Web?

Mltiples tecnologas para hacer lo


mismo:
No interoperables entre s.
Ligados a una plataforma.

DCOM CORBA Java RMI


Protocolo RPC IIOP IIOP or JRMP
Formato del
NDR CDR Java Ser. Format
mensaje
Descripcin IDL OMG IDL Java
RMI Registry or
Descubrimiento Windows Registry Naming Service
JNDI

16 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 8


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Pila de protocolos de SW

Redefinicin de toda la pila de


comunicaciones
Basado en tecnologas estndares

Servicio web
Protocolo HTTP
Formato del
SOAP
mensaje
Descripcin WSDL
Descubrimiento UDDI

17 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Ventajas de los Servicios Web


Basados en estndares.
Fcil integracin.

Desarrollo de actividades modularizadas.

Independencia de plataforma.

Puede ser usado tanto en clientes ligeros


como pesados (clientes heterogneos).

18 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 9


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Desventajas de los Servicios Web

Es que no son seguros...

Es que no tienen estado...

Es que no son transaccionales...

Los servicios Web no hacen ms que reinventar la rueda,


pero esta vez usando XML.

19 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Protocolos Servicios Web

Publicar, buscar servicios: UDDI


Descripcin de servicios: WSDL
Interaccin de servicios: SOAP
Formato de datos universal: XML
Comunicaciones ubicuas: Internet

20 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 10


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Servicios Web con Java

Los servicios Web con Java se manejan dentro del JWSDP que apartir
del JDK 1.4 ya viene integrado de forma nativa al JSE

Para implementar un Servicio Web se ocupa de un Servidor Web.

21 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Servicios Web con Java

El servidor Web debe soportar Servicios Web. Generalmente se


agregan a servidores Web tradicionales contenedores de servicios
Web (axis, metro)

Para poder consumir un servicio Web slo se ocupa conocer la


implementacin del servicio y que est activo.

22 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 11


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Servicios Web con Java

Los Servicios Web se pueden manejar fcilmente utilizando IDEs


como Netbeans o Eclipse.

Las tecnologas que se involucran para el manejo de servicios Web


con Java son muchas: JAX-WS, JAX-B, WS-Metadata, JAX-RS, etc.

23 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Arquitectura Java WS

24 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 12


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Comunicacin WS

25 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

Es la API ms reciente de Java para el manejo de Servicios Web.

A partir de JSE 6.0 se encuentra de manera nativa, ya que


anteriormente estaba en JEE.

26 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 13


UNIVERSIDAD ALAS PERUANAS 10/12/2014

JAX-WS

Reemplaza la anterior API de JAX RPC.

Incluye nuevos paquetes como: javax.xml.ws, javax.xml.soap,javax.jws

Utiliza anotaciones para el manejo de servicios Web.

27 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

La anotacin @WebService indica que una clase se expondr como


Servicio Web.

@WebMethod indica que un mtodo se expondr como interfaz


pblica a utilizarse en un servicio Web.

28 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 14


UNIVERSIDAD ALAS PERUANAS 10/12/2014

JAX-WS

Las anotaciones pueden recibir parmetros. Por ejemplo


@WebService puede ir acompaado de parmetros como name,
serviceName, targetNamespace, portName, entre otros.

29 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

Crear Servicios Web es sencillo teniendo implementado la lgica del


negocio.

Solo es necesario agregar el paquete:

import javax.jws.WebService;

30 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 15


UNIVERSIDAD ALAS PERUANAS 10/12/2014

JAX-WS

La clase del servicio se le antepone @WebService.

A continuacin se muestra un ejemplo tomado del tutorial oficial de


oracle:

package hello;
public class CircleFunctions {

31 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

public double getArea(double radius) {


return java.lang.Math.PI * (r * r); }

public double getCircumference(double radius) { return 2 *


java.lang.Math.PI * r; } }

32 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 16


UNIVERSIDAD ALAS PERUANAS 10/12/2014

JAX-WS

El servicio publicado quedara:

package hello;
import javax.jws.WebService; @WebService
public class CircleFunctions {
public double getArea(double r) { return java.lang.Math.PI * (r * r); }

33 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

public double getCircumference(double r) { return 2 * java.lang.Math.PI *


r; } }

Se necesita publicar el mtodo, esto se hace con el mtodo publish()


de la clase Endpoint:

34 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 17


UNIVERSIDAD ALAS PERUANAS 10/12/2014

JAX-WS

import javax.xml.ws.Endpoint;
public static void main(String[] args) {
Endpoint.publish(
"http://localhost:8080/WebServiceExample/circlefunctions", new
CircleFunctions()); }

35 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

Ahora se pueden compilar los cdigos fuentes pero antes es necesario


crear la estructura del servicio a treavs de:

Wsgen cp hello.CircleFunctions

36 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 18


UNIVERSIDAD ALAS PERUANAS 10/12/2014

JAX-WS

Para comprobar que el servicio Web est corriendo se debe de visitar:

http://localhost:8080/WebServiceExample/circlefunctions?WSDL

Que es la descripcin del servicio

37 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-WS

En Netbeans los servicios Web se implementan dentro de proyectos


Web.

Muchas de estas tareas se simplifican con NetBeans.

Los Clientes que consumen servicios web se puden hacer desde


cualquier tipo de proyecto

38 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 19


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Es la Arquitectura de Java para el ligado


(binding) en XML.

Es utilizado para el marshalling de los datos


de Java con otros lenguajes utilizando
generalmente XMLSchema o DTD para
representar los datos que se utilizan en
WSDL y SOAP.

39
JAX-B
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

JAX-B

Se pueden utilizar datos primitivos o definidos por el usuario (datos


complejos).

Est API depende de modelos de lectura DOM o SAX de los archivos


de XML.

40 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 20


UNIVERSIDAD ALAS PERUANAS 10/12/2014

Tipos de Datos Bsicos

XMLSchema Java
xsd:string java.lang.String
xsd:integer java.math.BigInteger
xsd:int int
xsd.long long
xsd:short short
xsd:decimal java.math.BigDecimal
xsd:float float

41 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Dudas

42 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 21


UNIVERSIDAD ALAS PERUANAS 10/12/2014

43 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

44 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Lic. Vladimir Cotaquispe Gutierrez 22

También podría gustarte