From 7e143670325257a8ae15c642688de9b056ea245d Mon Sep 17 00:00:00 2001 From: michada Date: Mon, 10 Feb 2014 22:59:25 +0100 Subject: [PATCH] RESTful API correction. RESTful API modified to use PUT, DELETE, POST and GET methods. --- .../java/es/uvigo/esei/daa/dao/PeopleDAO.java | 24 ++++++++ .../es/uvigo/esei/daa/entities/Person.java | 3 + .../java/es/uvigo/esei/daa/rest/People.java | 43 ++++++++----- src/main/webapp/WEB-INF/web.xml | 60 ++++++++++++------- src/main/webapp/js/dao/people.js | 46 +++++++++----- src/main/webapp/js/view/people.js | 9 +-- 6 files changed, 128 insertions(+), 57 deletions(-) diff --git a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java index 1c7ffe0..b02a754 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java @@ -11,6 +11,30 @@ import java.util.List; import es.uvigo.esei.daa.entities.Person; public class PeopleDAO extends DAO { + public Person get(int id) throws DAOException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM people WHERE id=?"; + + try (PreparedStatement statement = conn.prepareStatement(query)) { + statement.setInt(1, id); + + try (ResultSet result = statement.executeQuery()) { + if (result.next()) { + return new Person( + result.getInt("id"), + result.getString("name"), + result.getString("surname") + ); + } else { + throw new DAOException("Person not found"); + } + } + } + } catch (SQLException e) { + throw new DAOException(e); + } + } + public List list() throws DAOException { try (final Connection conn = this.getConnection()) { try (Statement statement = conn.createStatement()) { diff --git a/src/main/java/es/uvigo/esei/daa/entities/Person.java b/src/main/java/es/uvigo/esei/daa/entities/Person.java index d7926b9..05f53a4 100644 --- a/src/main/java/es/uvigo/esei/daa/entities/Person.java +++ b/src/main/java/es/uvigo/esei/daa/entities/Person.java @@ -5,6 +5,9 @@ public class Person { private String name; private String surname; + public Person() { + } + public Person(int id, String name, String surname) { this.id = id; this.name = name; diff --git a/src/main/java/es/uvigo/esei/daa/rest/People.java b/src/main/java/es/uvigo/esei/daa/rest/People.java index 9695cdf..ada32ca 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/People.java +++ b/src/main/java/es/uvigo/esei/daa/rest/People.java @@ -1,9 +1,13 @@ package es.uvigo.esei.daa.rest; +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -20,7 +24,6 @@ public class People { } @GET - @Path("/list") public Response list() { try { return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build(); @@ -29,11 +32,24 @@ public class People { return Response.serverError().entity(e.getMessage()).build(); } } - + @GET - @Path("/delete") + @Path("/{id}") + public Response get( + @PathParam("id") int id + ) { + try { + return Response.ok(this.dao.get(id), MediaType.APPLICATION_JSON).build(); + } catch (DAOException e) { + e.printStackTrace(); + return Response.serverError().entity(e.getMessage()).build(); + } + } + + @DELETE + @Path("/{id}") public Response delete( - @QueryParam("id") int id + @PathParam("id") int id ) { try { this.dao.delete(id); @@ -45,12 +61,12 @@ public class People { } } - @GET - @Path("/modify") + @PUT + @Path("/{id}") public Response modify( - @QueryParam("id") int id, - @QueryParam("name") String name, - @QueryParam("surname") String surname + @PathParam("id") int id, + @FormParam("name") String name, + @FormParam("surname") String surname ) { try { return Response.ok(this.dao.modify(id, name, surname)).build(); @@ -60,11 +76,10 @@ public class People { } } - @GET - @Path("/add") + @POST public Response add( - @QueryParam("name") String name, - @QueryParam("surname") String surname + @FormParam("name") String name, + @FormParam("surname") String surname ) { try { return Response.ok(this.dao.add(name, surname)).build(); diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 4a5a3c5..8f98f73 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -1,25 +1,39 @@ - - DAAExampleTMP - - index.html - index.htm - index.jsp - default.html - default.htm - default.jsp - - - DAA Example DB Connection - jdbc/daaexample - javax.sql.DataSource - Container - - - javax.ws.rs.core.Application - - - javax.ws.rs.core.Application - /rest/* - + + DAAExampleTMP + + index.html + index.htm + index.jsp + default.html + default.htm + default.jsp + + + DAA Example DB Connection + jdbc/daaexample + javax.sql.DataSource + Container + + + javax.ws.rs.core.Application + + com.sun.jersey.api.json.POJOMappingFeature + true + + + + javax.ws.rs.core.Application + /rest/* + + + \ No newline at end of file diff --git a/src/main/webapp/js/dao/people.js b/src/main/webapp/js/dao/people.js index c75389d..ab50d8c 100644 --- a/src/main/webapp/js/dao/people.js +++ b/src/main/webapp/js/dao/people.js @@ -3,10 +3,13 @@ function listPeople(done, fail, always) { fail = typeof fail !== 'undefined' ? fail : function() {}; always = typeof always !== 'undefined' ? always : function() {}; - $.getJSON('rest/people/list') - .done(done) - .fail(fail) - .always(always); + $.ajax({ + url: 'rest/people', + type: 'GET' + }) + .done(done) + .fail(fail) + .always(always); } function addPerson(person, done, fail, always) { @@ -14,10 +17,14 @@ function addPerson(person, done, fail, always) { fail = typeof fail !== 'undefined' ? fail : function() {}; always = typeof always !== 'undefined' ? always : function() {}; - $.getJSON('rest/people/add', person) - .done(done) - .fail(fail) - .always(always); + $.ajax({ + url: 'rest/people', + type: 'POST', + data: person + }) + .done(done) + .fail(fail) + .always(always); } function modifyPerson(person, done, fail, always) { @@ -25,10 +32,14 @@ function modifyPerson(person, done, fail, always) { fail = typeof fail !== 'undefined' ? fail : function() {}; always = typeof always !== 'undefined' ? always : function() {}; - $.getJSON('rest/people/modify', person) - .done(done) - .fail(fail) - .always(always); + $.ajax({ + url: 'rest/people/' + person.id, + type: 'PUT', + data: person + }) + .done(done) + .fail(fail) + .always(always); } function deletePerson(id, done, fail, always) { @@ -36,8 +47,11 @@ function deletePerson(id, done, fail, always) { fail = typeof fail !== 'undefined' ? fail : function() {}; always = typeof always !== 'undefined' ? always : function() {}; - $.getJSON('rest/people/delete', { 'id': id }) - .done(done) - .fail(fail) - .always(always); + $.ajax({ + url: 'rest/people/' + id, + type: 'DELETE', + }) + .done(done) + .fail(fail) + .always(always); } \ No newline at end of file diff --git a/src/main/webapp/js/view/people.js b/src/main/webapp/js/view/people.js index 1885530..d459d50 100644 --- a/src/main/webapp/js/view/people.js +++ b/src/main/webapp/js/view/people.js @@ -79,21 +79,22 @@ function enableForm() { $(peopleFormQuery + ' input').prop('disabled', false); } -function showErrorMessage(jqxhr, textStatus, error) { - alert(textStatus + ": " + error); -} - function resetForm() { $(peopleFormQuery)[0].reset(); $(peopleFormQuery + ' input[name="id"]').val(''); $('#btnSubmit').val('Crear'); } +function showErrorMessage(jqxhr, textStatus, error) { + alert(textStatus + ": " + error); +} + function addRowListeners(person) { $('#person-' + person.id + ' a.edit').click(function() { personToForm(rowToPerson(person.id)); $('input#btnSubmit').val('Modificar'); }); + $('#person-' + person.id + ' a.delete').click(function() { if (confirm('Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?')) { deletePerson(person.id, -- 2.18.1