Saltar la navegación

Cliente Rest

Objetivos

En esta sección crearemos un prototipo de cliente REST para consumir los Servicios Web expuestos por el Servidor. La imagen siguiente muestra la aplicación de escritorio que la realizaremos en Java. El propósito es codificar la lógica del botón "Consultar" para consumir el servicio web expuesto por el servidor.

Para esto necesitaremos:

  • Una clase Java que represente a la clase Persona del Servidor.- El código de esta clase se presenta en el fichero Persona.java.
  • Un formulario GUI desde la cual se gestionan los servicios (Operaciones CRUD expuestas por el servicio REST).- El código del Formulario se presenta en el fichero Principal.java. En esta clase debes codificar el método btnCargarPersonas().

Persona.java

import java.io.Serializable;


public class Persona implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Integer id;

	private String nombre;
	

	private String clave;


	public Persona() {

	}

	public Persona(String nombre, String clave) {
		this.nombre = nombre;
		this.clave = clave;
	}

	

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getClave() {
		return clave;
	}

	public void setClave(String clave) {
		this.clave = clave;
	}


	@Override
	public String toString() {
		return this.nombre+ ' '+ this.clave;
	}
	@Override
	public int hashCode() {
		int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
	}
	@Override
	public boolean equals(Object object) {
		// TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Persona)) {
            return false;
        }
        Persona other = (Persona) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
	}

}



Principal.java

public class Principal {

	private JFrame frmClienteRestPersona;
	private JPanel panelComandos;
	private JPanel panelTabla;
	private JTable tblPersonas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Principal window = new Principal();
					window.frmClienteRestPersona.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Principal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmClienteRestPersona = new JFrame();
		frmClienteRestPersona.setTitle("Cliente REST Persona");
		frmClienteRestPersona.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmClienteRestPersona.setLayout(new FlowLayout(FlowLayout.LEFT));
		frmClienteRestPersona.setSize(700, 300);
		frmClienteRestPersona.setVisible(true);

		// Panel Comandos
		panelComandos = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panelComandos.setBackground(new Color(250, 248, 185));
		panelComandos.setSize(680, 100);
		frmClienteRestPersona.add(panelComandos);
		// Botones
		JButton btnCargar = new JButton("Cargar...");
		panelComandos.add(btnCargar);
		btnCargar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnCargarPersonas();
			}
		});

		JButton btnInsertar = new JButton("Nuevo");
		panelComandos.add(btnInsertar);
		btnInsertar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});

		JButton btnActualizar = new JButton("Actualizar");
		panelComandos.add(btnActualizar);

		JButton btnEliminar = new JButton("Eliminar");
		panelComandos.add(btnEliminar);

		// Tabla
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("Id");
		model.addColumn("Nombre");
		model.addColumn("Clave");
		tblPersonas = new JTable(model);
		JScrollPane JS = new JScrollPane(tblPersonas);
		JS.setPreferredSize(new Dimension(680, 195));

		// Panel Tabla
		panelTabla = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panelTabla.setSize(680, 195);
		panelTabla.add(JS);
		frmClienteRestPersona.add(panelTabla);

	}

	private void btnCargarPersonas() {
		//TODO: A desarrollar!
	}

	private void btnInsertarPersona() {
	}

	private void btnEliminarPersona() {
	}
}



Creado con eXeLearning (Ventana nueva)