miércoles, 8 de julio de 2020

RESTful API

Crear un Web Service

Bibliotecas requeridas

  • JAX-RS 2.0
  • Jersey 2.5.1


Estructura del proyecto



Persona.java
package entity;

import java.time.LocalDate;

public class Persona {

    private String dni;
    private String name;
    private LocalDate birthday;
    private Float weight;

    public Persona() {
    }

    public String getDni() {
        return dni;
    }

    public void setDni(String dni) {
        this.dni = dni;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }

    public Float getWeight() {
        return weight;
    }

    public void setWeight(Float weight) {
        this.weight = weight;
    }    
}

RestApi.java
package apiRest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/service")
public class RestApi extends Application {

}

TextRest.java
package apiRest.services;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/texto")
@Produces(MediaType.TEXT_PLAIN)
public class TextRest {

    @GET
    @Path("/prueba")
    public String dataGet() {
        return "Probando un Web Service";
    }
}

JsonRest.java
package apiRest.services;

import entity.Persona;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public class JsonRest {

    @GET
    @Path("/objeto")
    public Response getObject() {
        Persona p = new Persona();
        p.setDni("96584422");
        p.setName("Paul");
        p.setBirthday(LocalDate.of(1993, 5, 17));
        p.setWeight(62.7f);
        return Response.ok(p).build();
    }
    
    @GET
    @Path("/lista")
    public Response getList() {
        Persona p1 = new Persona();
        p1.setDni("10084422");
        p1.setName("Milagros");
        p1.setBirthday(LocalDate.of(1993, 2, 7));
        p1.setWeight(52.7f);
        Persona p2 = new Persona();
        p2.setDni("00084422");
        p2.setName("Yadira");
        p2.setBirthday(LocalDate.of(1992, 10, 1));
        p2.setWeight(57f);
        Persona p3 = new Persona();
        p3.setDni("41557422");
        p3.setName("Alejandra");
        p3.setBirthday(LocalDate.of(1990, 1, 21));
        p3.setWeight(49.9f);
        List<Persona> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        GenericEntity<List<Persona>> ge = new GenericEntity<List<Persona>>(list){};
        return Response.ok(ge).build();
    }
}

Si colocamos http://localhost:8082/RestApi/service/texto/prueba en el navegador obtendremos la respuesta:

  • Probando un Web Service


Para consumir los Web Services que devuelven Json se debe invocar a sus URI en los clientes REST que serían:
  • http://localhost:8082/RestApi/service/json/lista
  • http://localhost:8082/RestApi/service/json/objeto


Nota: la dirección (localhost) y el puerto (8082) variarán dependiendo de la ubicación y puerto empleado por el servidor de Java EE.

No hay comentarios:

Publicar un comentario