Ing. Pablo Almeida Haro Msc.
RESTFUL WEB SERVICES
CREACION DEL SERVIDOR
CREACION DEL PROYECTO WEB
*Colocar click en file/new Project/java web/web application
*Ingresar nombre de proyecto y ubicación
*Ingresar servidor y versión de java
*No seleccionar ningún framework
GENERACION DE ENTITY CLASSES Y RESTFUL WEB SERVICES
*Click derecho sobre el proyecto new/other/web services/rest ful web services from database
*Seleccionar la tabla
*Ingresar el paquete
PROBAR LOS SERVICIOS WEB
*Click derecho sobre el proyecto/test restful web services
CREACION DE LA CAPA CLIENTE
*Crear un proyecto web: Seleccionar file new prject/web application
*Ingresar nombre y ubicación del proyecto
*Ingresar servidor y versión de java
CREACION DE LAS ENTIDADES DESDE LA BASE DE DATOS
*Click derecho sobre el proyecto/entity clases from database
CREACION DE LA OPERACIÓN USANDO RESTFUL
*Click derecho sobre el proyecto / new /other/web services/restful java client
*Click en browse y seleccionar el proyecto webservice
*Se creará automáticamente el código que sigue a continuación
package pkg_jersey;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
/**
* Jersey REST client generated for REST resource:ClienteFacadeREST
* [pkg_restful.cliente]<br>
* USAGE:
* <pre>
* clientejersey client = new clientejersey();
* Object response = client.XXX(...);
* // do whatever with response
* client.close();
* </pre>
*
* @author Administrador
*/
public class clientejersey {
private WebResource webResource;
private Client client;
private static final String BASE_URI = "http://localhost:8080/prj_restful/resources";
public clientejersey() {
com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
client = Client.create(config);
webResource = client.resource(BASE_URI).path("pkg_restful.cliente");
}
public void remove(String id) throws UniformInterfaceException {
webResource.path(java.text.MessageFormat.format("{0}", new Object[]{id})).delete();
}
public String countREST() throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path("count");
return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
}
public <T> T findAll_XML(Class<T> responseType) throws UniformInterfaceException {
WebResource resource = webResource;
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T findAll_JSON(Class<T> responseType) throws UniformInterfaceException {
WebResource resource = webResource;
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void edit_XML(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).put(requestEntity);
}
public void edit_JSON(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(requestEntity);
}
public void create_XML(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).post(requestEntity);
}
public void create_JSON(Object requestEntity) throws UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(requestEntity);
}
public <T> T findRange_XML(Class<T> responseType, String from, String to) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T findRange_JSON(Class<T> responseType, String from, String to) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public <T> T find_XML(Class<T> responseType, String id) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T find_JSON(Class<T> responseType, String id) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void close() {
client.destroy();
}
}
CREAR EL BACKING BEAN JSF
*Click derecho sobre el paquete pkg_cliente / new /java class
*Colocar el código que sigue a continuación
package pkg_cliente;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import pkg_jersey.clientejersey;
@ManagedBean()
@SessionScoped
public class cls_presentacion {
String codigo;
public String getCodigo() {
return codigo;
}
public void setCodigo(String codigo) {
this.codigo = codigo;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
String nombre;
public void insertar()
{
clientejersey client1=new clientejersey();
ClientResponse response=client1.findAll_XML(ClientResponse.class);
GenericType<List<Cliente>> genericType = new GenericType<List<Cliente>>() {};
List<Cliente> data= new ArrayList<Cliente>();
data=(response.getEntity(genericType));
Cliente p=new Cliente();
p.setCodigo(codigo);
p.setNombre(nombre);
client1.create_XML(p);
}
}
*En el index.xhtml colocar el código que sigue a continuación
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:outputText id="o_codigo" value="Codgo:"></h:outputText>
<h:inputText id="i_codigo" value="#{cls_presentacion.codigo}" ></h:inputText>
<br></br>
<h:outputText id="o_nombre" value="Nombre:"></h:outputText>
<h:inputText id="i-nombre" value="#{cls_presentacion.nombre}" ></h:inputText>
<br></br>
<h:commandButton id="Insertar" value="Insertar" action="#{cls_presentacion.insertar()}"></h:commandButton>
</h:form>
</h:body>
</html>
*Correr la aplicación cliente con click derecho sobre el proyecto y run