From 1273bb4aeef13b8823acc2adfd3d8312735aaaae Mon Sep 17 00:00:00 2001 From: Magdalena Krause Date: Fri, 5 Jan 2018 16:51:47 -0300 Subject: [PATCH] BAEL-1277: RESTFul CRUD application with JavaLite (#3359) * BAEL-1277: RESTFul CRUD application with JavaLite. * BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling. --- java-lite/README.md | 2 + java-lite/pom.xml | 105 ++++++++++++++++++ .../main/java/app/config/AppBootstrap.java | 9 ++ .../java/app/config/AppControllerConfig.java | 15 +++ .../src/main/java/app/config/DbConfig.java | 11 ++ .../app/controllers/ProductsController.java | 101 +++++++++++++++++ .../src/main/java/app/models/Product.java | 7 ++ .../src/main/resources/database.properties | 4 + .../webapp/WEB-INF/views/products/_comma.ftl | 1 + .../WEB-INF/views/products/_product.ftl | 4 + .../webapp/WEB-INF/views/products/index.ftl | 1 + .../webapp/WEB-INF/views/products/message.ftl | 4 + java-lite/src/main/webapp/WEB-INF/web.xml | 25 +++++ .../src/test/java/app/models/ProductTest.java | 25 +++++ pom.xml | 1 + 15 files changed, 315 insertions(+) create mode 100644 java-lite/README.md create mode 100644 java-lite/pom.xml create mode 100644 java-lite/src/main/java/app/config/AppBootstrap.java create mode 100644 java-lite/src/main/java/app/config/AppControllerConfig.java create mode 100644 java-lite/src/main/java/app/config/DbConfig.java create mode 100644 java-lite/src/main/java/app/controllers/ProductsController.java create mode 100644 java-lite/src/main/java/app/models/Product.java create mode 100644 java-lite/src/main/resources/database.properties create mode 100644 java-lite/src/main/webapp/WEB-INF/views/products/_comma.ftl create mode 100644 java-lite/src/main/webapp/WEB-INF/views/products/_product.ftl create mode 100644 java-lite/src/main/webapp/WEB-INF/views/products/index.ftl create mode 100644 java-lite/src/main/webapp/WEB-INF/views/products/message.ftl create mode 100644 java-lite/src/main/webapp/WEB-INF/web.xml create mode 100644 java-lite/src/test/java/app/models/ProductTest.java diff --git a/java-lite/README.md b/java-lite/README.md new file mode 100644 index 0000000000..bcb84e186e --- /dev/null +++ b/java-lite/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [RESTFul CRUD application with JavaLite] () \ No newline at end of file diff --git a/java-lite/pom.xml b/java-lite/pom.xml new file mode 100644 index 0000000000..554819f6e4 --- /dev/null +++ b/java-lite/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + + org.baeldung + java-lite + 1.0-SNAPSHOT + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + 9.3.4.RC1 + 1.4.13 + 1.15 + 5.1.45 + 1.7.0 + 1.8.2 + 4.11 + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.maven.plugin.version} + + + + activejdbc.log + + + + active_reload + true + + + activeweb.log.request + true + + + + + + + org.javalite + activejdbc-instrumentation + ${activejdbc.version} + + + process-classes + + instrument + + + + + + + + + + org.javalite + activeweb + ${activeweb.version} + + + + mysql + mysql-connector-java + ${mysql.connector.java.version} + + + + com.sun + tools + ${sun.tools.version} + system + ${java.home}/../lib/tools.jar + + + + org.codehaus.jackson + jackson-core-lgpl + ${jackson.version} + + + org.codehaus.jackson + jackson-mapper-lgpl + ${jackson.version} + + + junit + junit + ${junit.version} + + + + \ No newline at end of file diff --git a/java-lite/src/main/java/app/config/AppBootstrap.java b/java-lite/src/main/java/app/config/AppBootstrap.java new file mode 100644 index 0000000000..7a87c2d0a7 --- /dev/null +++ b/java-lite/src/main/java/app/config/AppBootstrap.java @@ -0,0 +1,9 @@ +package app.config; + +import org.javalite.activeweb.AppContext; +import org.javalite.activeweb.Bootstrap; + +public class AppBootstrap extends Bootstrap { + public void init(AppContext context) { + } +} diff --git a/java-lite/src/main/java/app/config/AppControllerConfig.java b/java-lite/src/main/java/app/config/AppControllerConfig.java new file mode 100644 index 0000000000..42b7e728ec --- /dev/null +++ b/java-lite/src/main/java/app/config/AppControllerConfig.java @@ -0,0 +1,15 @@ +package app.config; + +import app.controllers.ProductsController; +import org.javalite.activeweb.AbstractControllerConfig; +import org.javalite.activeweb.AppContext; +import org.javalite.activeweb.controller_filters.DBConnectionFilter; +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); + } +} diff --git a/java-lite/src/main/java/app/config/DbConfig.java b/java-lite/src/main/java/app/config/DbConfig.java new file mode 100644 index 0000000000..25ba378b22 --- /dev/null +++ b/java-lite/src/main/java/app/config/DbConfig.java @@ -0,0 +1,11 @@ +package app.config; + +import org.javalite.activeweb.AbstractDBConfig; +import org.javalite.activeweb.AppContext; + +public class DbConfig extends AbstractDBConfig { + @Override + public void init(AppContext appContext) { + 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 new file mode 100644 index 0000000000..f68dd9a013 --- /dev/null +++ b/java-lite/src/main/java/app/controllers/ProductsController.java @@ -0,0 +1,101 @@ +package app.controllers; + +import app.models.Product; +import org.codehaus.jackson.map.ObjectMapper; +import org.javalite.activeweb.AppController; +import org.javalite.activeweb.annotations.RESTful; + +import java.util.Map; + +@RESTful +public class ProductsController extends AppController { + + 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"); + } + } + + 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"); + } + } + + 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; + } + 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; + } + 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; + } + 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"; + } + + @Override + protected String getLayout() { + return null; + } +} diff --git a/java-lite/src/main/java/app/models/Product.java b/java-lite/src/main/java/app/models/Product.java new file mode 100644 index 0000000000..7fa32b75d9 --- /dev/null +++ b/java-lite/src/main/java/app/models/Product.java @@ -0,0 +1,7 @@ +package app.models; + +import org.javalite.activejdbc.Model; + +public class Product extends Model { + +} diff --git a/java-lite/src/main/resources/database.properties b/java-lite/src/main/resources/database.properties new file mode 100644 index 0000000000..55b3851d33 --- /dev/null +++ b/java-lite/src/main/resources/database.properties @@ -0,0 +1,4 @@ +development.driver=com.mysql.jdbc.Driver +development.username=user +development.password=password +development.url=jdbc:mysql://localhost/dbname \ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/views/products/_comma.ftl b/java-lite/src/main/webapp/WEB-INF/views/products/_comma.ftl new file mode 100644 index 0000000000..41622b4720 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/products/_comma.ftl @@ -0,0 +1 @@ +, \ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/views/products/_product.ftl b/java-lite/src/main/webapp/WEB-INF/views/products/_product.ftl new file mode 100644 index 0000000000..562af0499e --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/products/_product.ftl @@ -0,0 +1,4 @@ +{ +"id" : ${product.id}, +"name" : "${product.name}" +} \ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/views/products/index.ftl b/java-lite/src/main/webapp/WEB-INF/views/products/index.ftl new file mode 100644 index 0000000000..c343f20910 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/products/index.ftl @@ -0,0 +1 @@ +[<@render partial="product" collection=products spacer="comma"/>] \ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/views/products/message.ftl b/java-lite/src/main/webapp/WEB-INF/views/products/message.ftl new file mode 100644 index 0000000000..3e7faf1459 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/products/message.ftl @@ -0,0 +1,4 @@ +{ +"message" : "${message}", +"code" : ${code} +} \ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/web.xml b/java-lite/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..c285876c86 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,25 @@ + + + + + + dispatcher + org.javalite.activeweb.RequestDispatcher + + exclusions + css,images,js,ico + + + encoding + UTF-8 + + + + + + dispatcher + /* + + + diff --git a/java-lite/src/test/java/app/models/ProductTest.java b/java-lite/src/test/java/app/models/ProductTest.java new file mode 100644 index 0000000000..f6ee0a3d0a --- /dev/null +++ b/java-lite/src/test/java/app/models/ProductTest.java @@ -0,0 +1,25 @@ +package app.models; + +import org.javalite.activejdbc.Base; +import org.junit.Assert; +import org.junit.Test; + +public class ProductTest { + + //@Test + public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() { + //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(); + + //Find product + Product savedProduct = Product.findFirst("name = ?", "Bread"); + + Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name")); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 19e42009c9..7022fa3a38 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,7 @@ jackson vavr + java-lite javax-servlets javaxval jaxb