From cbd1a9dfbfdcc71200ae211f5af8bc56fd919d09 Mon Sep 17 00:00:00 2001 From: Magdalena Krause Date: Mon, 8 Jan 2018 18:34:16 -0300 Subject: [PATCH] Bael 1277 (#3379) * BAEL-1277: RESTFul CRUD application with JavaLite. * BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling. * BAEL-1277: Changes after editors review. --- java-lite/pom.xml | 13 +- .../java/app/config/AppControllerConfig.java | 4 +- .../src/main/java/app/config/DbConfig.java | 2 +- .../app/controllers/ProductsController.java | 128 +++++++++--------- .../src/test/java/app/models/ProductTest.java | 18 +-- 5 files changed, 79 insertions(+), 86 deletions(-) diff --git a/java-lite/pom.xml b/java-lite/pom.xml index 554819f6e4..eb18bc40a5 100644 --- a/java-lite/pom.xml +++ b/java-lite/pom.xml @@ -7,6 +7,7 @@ org.baeldung java-lite 1.0-SNAPSHOT + war com.baeldung @@ -15,7 +16,7 @@ - 9.3.4.RC1 + 9.4.8.v20171121 1.4.13 1.15 5.1.45 @@ -85,16 +86,6 @@ ${java.home}/../lib/tools.jar - - org.codehaus.jackson - jackson-core-lgpl - ${jackson.version} - - - org.codehaus.jackson - jackson-mapper-lgpl - ${jackson.version} - junit junit diff --git a/java-lite/src/main/java/app/config/AppControllerConfig.java b/java-lite/src/main/java/app/config/AppControllerConfig.java index 42b7e728ec..da30c08ab2 100644 --- a/java-lite/src/main/java/app/config/AppControllerConfig.java +++ b/java-lite/src/main/java/app/config/AppControllerConfig.java @@ -9,7 +9,7 @@ import org.javalite.activeweb.controller_filters.TimingFilter; public class AppControllerConfig extends AbstractControllerConfig { @Override public void init(AppContext appContext) { - addGlobalFilters(new TimingFilter()); - add(new DBConnectionFilter()).to(ProductsController.class); + addGlobalFilters(new TimingFilter()); + add(new DBConnectionFilter()).to(ProductsController.class); } } diff --git a/java-lite/src/main/java/app/config/DbConfig.java b/java-lite/src/main/java/app/config/DbConfig.java index 25ba378b22..75de248619 100644 --- a/java-lite/src/main/java/app/config/DbConfig.java +++ b/java-lite/src/main/java/app/config/DbConfig.java @@ -6,6 +6,6 @@ import org.javalite.activeweb.AppContext; public class DbConfig extends AbstractDBConfig { @Override public void init(AppContext appContext) { - this.configFile("/database.properties"); + this.configFile("/database.properties"); } } diff --git a/java-lite/src/main/java/app/controllers/ProductsController.java b/java-lite/src/main/java/app/controllers/ProductsController.java index f68dd9a013..746d77e24a 100644 --- a/java-lite/src/main/java/app/controllers/ProductsController.java +++ b/java-lite/src/main/java/app/controllers/ProductsController.java @@ -10,92 +10,94 @@ import java.util.Map; @RESTful public class ProductsController extends AppController { + private ObjectMapper mapper = new ObjectMapper(); + public void index() { - try { - view("products", Product.findAll()); - render().contentType("application/json"); - } catch (Exception e) { - view("message", "There was an error.", "code", 200); - render("message"); - } + try { + view("products", Product.findAll()); + render().contentType("application/json"); + } catch (Exception e) { + view("message", "There was an error.", "code", 200); + render("message"); + } } public void create() { - try { - Map payload = new ObjectMapper().readValue(getRequestString(), Map.class); - Product p = new Product(); - p.fromMap(payload); - p.saveIt(); - view("message", "Successfully saved product id " + p.get("id"), "code", 200); - render("message"); - } catch (Exception e) { - view("message", "There was an error.", "code", 200); - render("message"); - } + try { + Map payload = mapper.readValue(getRequestString(), Map.class); + Product p = new Product(); + p.fromMap(payload); + p.saveIt(); + view("message", "Successfully saved product id " + p.get("id"), "code", 200); + render("message"); + } catch (Exception e) { + view("message", "There was an error.", "code", 200); + render("message"); + } } public void update() { - try { - Map payload = new ObjectMapper().readValue(getRequestString(), Map.class); - String id = getId(); - Product p = Product.findById(id); - if (p == null) { - view("message", "Product id " + id + " not found.", "code", 200); - render("message"); - return; + try { + Map payload = mapper.readValue(getRequestString(), Map.class); + String id = getId(); + Product p = Product.findById(id); + if (p == null) { + view("message", "Product id " + id + " not found.", "code", 200); + render("message"); + return; + } + p.fromMap(payload); + p.saveIt(); + view("message", "Successfully updated product id " + id, "code", 200); + render("message"); + } catch (Exception e) { + view("message", "There was an error.", "code", 200); + render("message"); } - p.fromMap(payload); - p.saveIt(); - view("message", "Successfully updated product id " + id, "code", 200); - render("message"); - } catch (Exception e) { - view("message", "There was an error.", "code", 200); - render("message"); - } } public void show() { - try { - String id = getId(); - Product p = Product.findById(id); - if (p == null) { - view("message", "Product id " + id + " not found.", "code", 200); - render("message"); - return; + try { + String id = getId(); + Product p = Product.findById(id); + if (p == null) { + view("message", "Product id " + id + " not found.", "code", 200); + render("message"); + return; + } + view("product", p); + render("_product"); + } catch (Exception e) { + view("message", "There was an error.", "code", 200); + render("message"); } - view("product", p); - render("_product"); - } catch (Exception e) { - view("message", "There was an error.", "code", 200); - render("message"); - } } public void destroy() { - try { - String id = getId(); - Product p = Product.findById(id); - if (p == null) { - view("message", "Product id " + id + " not found.", "code", 200); - render("message"); - return; + try { + String id = getId(); + Product p = Product.findById(id); + if (p == null) { + view("message", "Product id " + id + " not found.", "code", 200); + render("message"); + return; + } + p.delete(); + view("message", "Successfully deleted product id " + id, "code", 200); + render("message"); + } catch (Exception e) { + view("message", "There was an error.", "code", 200); + render("message"); } - p.delete(); - view("message", "Successfully deleted product id " + id, "code", 200); - render("message"); - } catch (Exception e) { - view("message", "There was an error.", "code", 200); - render("message"); - } } @Override protected String getContentType() { - return "application/json"; + return "application/json"; } @Override protected String getLayout() { - return null; + return null; } } diff --git a/java-lite/src/test/java/app/models/ProductTest.java b/java-lite/src/test/java/app/models/ProductTest.java index f6ee0a3d0a..5e5c6e8845 100644 --- a/java-lite/src/test/java/app/models/ProductTest.java +++ b/java-lite/src/test/java/app/models/ProductTest.java @@ -8,18 +8,18 @@ public class ProductTest { //@Test public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() { - //Open DB connection - Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password"); + //Open DB connection + Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password"); - //Create a product and save it - Product toSaveProduct = new Product(); - toSaveProduct.set("name", "Bread"); - toSaveProduct.saveIt(); + //Create a product and save it + Product toSaveProduct = new Product(); + toSaveProduct.set("name", "Bread"); + toSaveProduct.saveIt(); - //Find product - Product savedProduct = Product.findFirst("name = ?", "Bread"); + //Find product + Product savedProduct = Product.findFirst("name = ?", "Bread"); - Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name")); + Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name")); } } \ No newline at end of file