Giter Site home page Giter Site logo

serviciosrest's Introduction

ServiciosREST

Ejemplo de implementación de servicios REST en java con JAX-RS.

URL Consumo de servicios ⚙️

POST

HTTP/1.1

  • URL: /ServiciosREST/app/restServices/pruebaPOST
  • Host: localhost:8080
  • Content-Type: application/json
  • body: {"nombre":"Sebastian"}

GET

HTTP/1.1

  • URL: /ServiciosREST/app/restServices/pruebaGET
  • Host: localhost:8080

Paso a paso 🔧

  • Crear proyecto Java WEB.

  • Agregar las librarías de:

- JAX-RS 2.0

- Jersey 2.5.1 (JAX-RS RI)
  • Se crean las clases para configuración y servicios.

Configuración:

package XXXX;

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

@ApplicationPath("XXXXXX")
public class RestConf extends Application {

}

Servicios

package REST;

import DTO.Respuesta;
import DTO.Usuario;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

Path("XXXXXXXXXX")
public class RestServices {

    @POST
    @Path("pruebaPOST")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    public Respuesta pruebaPOST(Usuario usuario) {
        Respuesta rta = new Respuesta();
        rta.setDescripcion("Prueba POST OK. nombre: " + usuario.getNombre());
        return rta;
    }

    @GET
    @Path("pruebaGET")
    @Produces({MediaType.APPLICATION_JSON})
    public String pruebaGET() {
        return "Prueba GET OK.";
    }
}
  • Se deben crear para el servicio POST dos DTO para el manejo de Consumes y Produces
package DTO;
public class Respuesta {

    private int codigo;
    private String descripcion;

    /**
    * @return the codigo
    */
    public int getCodigo() {
        return codigo;
    }

    /**
    * @param codigo the codigo to set
    */
    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    /**
    * @return the descripcion
    */
    public String getDescripcion() {
        return descripcion;
    }

    /**
    * @param descripcion the descripcion to set
    */
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
}

package DTO;

public class Usuario {
    private String nombre;

    /**
    * @return the nombre
    */
    public String getNombre() {
        return nombre;
    }

    /**
    * @param nombre the nombre to set
    */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
}

Persistencia a BD PostgreSQL con JDBC

  • Agregar la libreria
- PostgreSQL JDBC Driver
  • Crear la clase para la conexión a la BD

Ejemplo

package Conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConexionBD {

    public static Connection connection;
    private static String user = "userDb";
    private static String password = "contraseñaBD";

    public Connection conectar() {
        try {
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection("jdbc:postgresql://[IP]:[Port]/[name DB]", user, password);
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Error de conexion PostgreSQL" + e);
        }
        return connection;
    }

    public void desconectarse() {
        try {
            connection.close();
        } catch (SQLException e) {
            System.out.println("Error al desconectar PostgreSQL " + e);
        }
    }
}
  • Se crea la Interface para el manejo de metodos dentro de la clase para el DAO

Ejemplo

package Interface;

import DTO.Respuesta;

public interface interfaceUser {

    public abstract Respuesta getUsers();
}
  • Se crea la clase para la implementacion de la conexion JDBC

Ejemplo

package DAO;

import Conexion.ConexionBD;
import DTO.Respuesta;
import DTO.Usuario;
import Interface.interfaceUser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserDAO implements interfaceUser {

    private ConexionBD cn = new ConexionBD();
    
    @Override
    public Respuesta getUsers() {
        Respuesta rta = new Respuesta();
        ArrayList<Object> objetoRespuesta = new ArrayList<>();
        String consultaSql = "SELECT * FROM usuario;";
        try {
            PreparedStatement ps = cn.conectar().prepareStatement(consultaSql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Usuario usuario = new Usuario();
                usuario.setNombre(rs.getString("NOMBRE"));
                objetoRespuesta.add(usuario);
            }
            rta.setCodigo(1);
            rta.setDescripcion("Consulta Exitosa.");
            rta.setObjetoRespuesta(objetoRespuesta);
        } catch (SQLException e) {
            rta.setCodigo(0);
            rta.setDescripcion("Error obtenerUsuario: " + e);
        } finally {
            cn.desconectarse();
        }
        return rta;
    }
}

Estructura 🔩

ScreenShot

Más documentación 📖

Autor ✒️

También puedes mirar la lista de todos los contribuyentes quíenes han participado en este proyecto.


⌨️ por SebastianP07

serviciosrest's People

Contributors

sebastianp07 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.